Question

In: Computer Science

Write a odd and Even Array List in java script. plz ///explain cleary and plz give...

Write a odd and Even Array List in java script.

plz ///explain cleary and plz give a correct which run; postedbefore got code but it does not run

Solutions

Expert Solution

The Javascript code to seperate even, odd numbers from a list is:

//Creating a array with some numbers
var nums = [11,29,32,24,55,94,33,1,19,47,54,66,17];

//Creating two more arrays to store even and odd numbers seperately
var evens = [];
var odds = [];

//A function evenNumbers(nums) t seperate even,odd numbers in nums list
var evenNumbers = function(nums) {
  //Iterating through every number in nums list using for loop
  //nums.length returns length of nums array
    for (var i = 0; i < nums.length; i++) {

      //If the number at index i in nums array is divisible by 2 then it is even.
      //a%b gives the remainder when a is divided by b
        if ((nums[i] % 2) == 0) {
          //Adding this number to even array
            evens.push(nums[i]);
        }
        //Else it is odd number
        else {
          //Adding this number to odd array
            odds.push(nums[i]); 
        }
    }

};

//calling evenNumbers() function
evenNumbers(nums);

//Displaying even numbers, odd numbers arrays seperately
console.log("Even numbers in given list: ");
console.log(evens);
console.log("Odd numbers in given list: ");
console.log(odds);

I have declared and initialized a array called nums[] with some numbers. The I have created two more empty arrays even[ ] and odd[ ] to store the even and odd numbers of nums[] array seperately. I have called a function evenNumbers(nums) which checks if a number in the nums[] array is even or odd by dividing it by 2 and checking if remainder is 0. If remainder is 0 then it will add the number to even[ ] array using push() function else it will add number to odds[] array using push() function. We check this to all numbers in array nums by using a for loop that iterates till loop variable is less than length of nums[ ] array.

Then i have printed the output to the console using console() function. I have provided comments in the code explaining the process. I have tested the code and validated output. I am sharing output screenshot for your reference.

You can change numbers in the list nums[] and check for different cases.

Hope this answer helps you.

Thank you :)


Related Solutions

Write a java script function that accepts integer array as input, and display a new array...
Write a java script function that accepts integer array as input, and display a new array by performing fol lowing modifications, • The values in odd index positions must be incremented by 1 • The values in even index positions must be decremented by 1. • Assume the array index starts from 0. Input 1: arr = [1,2,3,4] Output 1: arr = [0,3,2,5 it done html and javascript and plz do it in simple way
Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
[JAVA SCRIPT] Please create an array of student names and another array of student grades. Create...
[JAVA SCRIPT] Please create an array of student names and another array of student grades. Create a function that can put a name and a grade to the arrays. Keep Read student name and grade until student name is “???”. And save the reading by using a function Create another function to show all the grade in that object. Create the third function that can display the maximum grade and the student’s name. Create a sorting function that can sort...
Write a java program: Write a nested loop program creating an array list of three positions.  The...
Write a java program: Write a nested loop program creating an array list of three positions.  The first loop terminates with a sentinel value of which user is prompted for input to continue of quit.   Inside loop to enter in each position a prompted input of person's first name.  After the inside loop, edit the second array location making its value "Boston". Print the array's contents.
JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
In Java Script how do you code an array with the values and suits of cards...
In Java Script how do you code an array with the values and suits of cards (all 52 cards in a deck) 2 different array one that just has the face values of the card, and the other with the suit value. Then have it randomly give you 1-2 cards from the deck without repeating? (Example: Dealer gives player two cards. Player asks for a hit and the dealer gives a different card, no repeats). Game is Blackjack.
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT