dimanche 1 août 2021

How to create new arrays of even, odd and large numbers from an array and print their values sequentially? - must be use callback function to do this

My Inputted Array is

var userArray = [4, 7, 2, 8, 9, 10, 15, 5, 1, 6, 12].

My expected output is:

var evenArray = [4,2,8,10,6,12]
var oddArray = [7,9,15,5,1]
var greaterNumbArray = [7,8,9,10,15,12]
All values between 3 arrays- 4 2 8 10 6 12 7 9 15 5 1 7 8 9 10 15 12.

The task is:

  1. Find the even, odd, and numbers greater than 6 and make an even & odd number array differently, last make an array of numbers greater than 6.

  2. Show the results of 3 arrays serially.

  3. Show the values of even, odd and large numbers together consistently.

Note: The callback function must be used.

Here is my code-

function behindOperation_2(arrayValue_2){
  var allArray_2 = [
    evenArray_2 = [],
    oddArray_2 = [],
    greaterArray_2 = []
  ]
  for(var i = 0; i<arrayValue_2.length; i++){
    if (arrayValue_2[i] % 2 === 0){
      allArray_2[0].push(arrayValue_2[i])
    }
    if(arrayValue_2[i] % 2 === 1){
      allArray_2[1].push(arrayValue_2[i])
    }
    if(arrayValue_2[i] > 6){
      allArray_2[2].push(arrayValue_2[i])
    }
  }
  for(var a = 0; a < allArray_2.length; a++){
    for(var v = 0; v<allArray_2[a].length; v++){
     console.log (allArray_2[a][v])
    }
  }
}

function dataFinding (getArray, behindOperation){
  var behindResult = behindOperation(getArray)
  return behindResult
}

var newResult_2 = dataFinding(userArray, behindOperation_2)
console.log(newResult_2);
My output is: 4 2 8 10 6 12 7 9 15 5 1 7 8 9 10 15 12

Aucun commentaire:

Enregistrer un commentaire