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.
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
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...
Array & Function - Can I pass a single parameter to a function that takes two...
Array & Function - Can I pass a single parameter to a function that takes two arguments: data passed from: arrayOne(4); to function: string function (array[], int)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT