Question

In: Computer Science

6.15 LAB: JavaScript arrays Write the function divideArray() in script.js that has a single numbers parameter...

6.15 LAB: JavaScript arrays

Write the function divideArray() in script.js that has a single numbers parameter containing an array of integers. The function should divide numbers into two arrays, evenNums for even numbers and oddNums for odd numbers. Then the function should sort the two arrays and output the array values to the console.

Ex: The function call:

var nums = [4, 2, 9, 1, 8];
divideArray(nums);

produces the console output:

Even numbers:
2
4
8
Odd numbers:
1
9

The program should output "None" if no even numbers exist or no odd numbers exist.

Ex: The function call:

var nums = [4, 2, 8];
divideArray(nums);

produces the console output:

Even numbers:
2
4
8
Odd numbers:
None

Hints: Use the push() method to add numbers to the evenNums and oddNums arrays. Supply the array sort() method a comparison function for sorting numbers correctly.

To test your code in your web browser, call divideArray() from the JavaScript console.

Solutions

Expert Solution

script.js :

//function divideArray()

        function divideArray(nums) {

            //declaring array to store oddNumber

            var oddArray = [];

            //this is the array which stores even number

            var evenArray = [];

            //using for loop

            for (var i = 0; i < nums.length; i++) {

                //checking value of i

                if (nums[i] % 2 == 0) {

                    //when even number is found then

                    //push element into even array

                    evenArray.push(nums[i]);

                }

                else {

                    //when odd number is found then

                    //push element into odd array

                    oddArray.push(nums[i]);

                }

            }

            //test the odd and even array

            evenArray.sort();

            oddArray.sort();

            //print even numbers

            console.log("Even numbers:");

            //checking array length

            if (evenArray.length > 0) {

                //when evenArray conatins elements then

                //print even numbers

                //using for loop

                for (var i = 0; i < evenArray.length; i++) {

                    console.log(evenArray[i]);//print element

                }

            }

            else {

                //when evenArray has no any element then print

                console.log("None");

            }

            //print odd numbers

            console.log("Odd numbers:");

            //checking array length

            if (oddArray.length > 0) {

                //when oddArray conatins elements then

                //print odd numbers

                //using for loop

                for (var i = 0; i < oddArray.length; i++) {

                    console.log(oddArray[i]);//print element

                }

            }

            else {

                //when oddArray has no any element then print

                console.log("None");

            }

        }

        //declare array with numbers

        var nums = [4, 2, 9, 1, 8];

        //call function divideArray()

        divideArray(nums);

======================================

Screen when function is called with array  [4, 2, 9, 1, 8] :

Screen when function is called with array  [4, 2, 8] :


Related Solutions

Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list.
In this lab we will write 3 functions: GenData: This function will take an integer parameter...
In this lab we will write 3 functions: GenData: This function will take an integer parameter and return a vector with that many random integers generated in the range of 0 and 100 (rand()%101). Seed the random number generator with 22. Mean(): This function will take a vector and return the mean. Variance(): This function will take a vector and return the population variance, as: [Sum for values[( x_i - Mean )^2]] / (number of items) In Main: Use GenData...
Write a JavaScript program with a function named fives that reads two numbers from two text...
Write a JavaScript program with a function named fives that reads two numbers from two text fields and then outputs to a div "True" if both of the numbers are greater than 5 or if their sum is greater than 20. Otherwise your function should output "False" to the div. If you wish, you may use the following HTML code to begin your program.
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON...
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON encoding). The parameter encodes an array of Numbers. Your function should display the value at index 0 of the array in a text element whose id is "small", display the value at index 1 of the array in a text element whose id is "med", and display the value at index 2 of the array in a text element whose id is "large".
Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
Write a JavaScript program that computes the average of a collection of numbers and then outputs...
Write a JavaScript program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the second highest number from the array. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input array will always have length at least two (so there is always a second highest value). The original array must NOT be modified by this function. Example secondHighest([5,3,8,6,2,7,4]) must return 7, and secondHighest([5,3,8,6,2,7,8]) must return 8. I have...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT