Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Problem:
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.

The callback function must be used.


My inputted array:

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

My expected output:

JavaScript
evenArray = [4,2,8,10,6,12]
oddArray = [7,9,15,5,1]
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


What I have tried:

Here is my code:

JavaScript
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);


output is: 4 2 8 10 6 12 7 9 15 5 1 7 8 9 10 15 12
Posted
Updated 9-Aug-21 8:57am

Since you didn't stated any problem, just a little improvement :
JavaScript
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])
  }
  // print an end of line here to separate the 3 lists
}
 
Share this answer
 
Comments
Swadip Singho Roy 8-Aug-21 15:58pm    
@Patrice I want to do all the operations from the dataFinding function
Patrice T 8-Aug-21 16:01pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Assuming your homework doesn't forbid it, use Array.filter:
Array.prototype.filter() - JavaScript | MDN[^]

Eg:
JavaScript
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
In this example, word => word.length > 6 is the callback function.
 
Share this answer
 
Separating Odd from Even - will solve part of your question
myEven = []; myOdd = [];
x = myArr.length; y = 0;
for (var j = 0; j <= x; j++ ) {
y = (j % 2) // if 0 even, if remainder 1 odd
if (y==0) {
myEven.push(j)
console.log("Even: "+j) ; }
else { myOdd.push(j)
console.log("Odd: "+j); }}
myEven.splice(0, 1); // remove 0 (0/2 = 0)
console.log("Even numbers: "+myEven);
console.table("Odd numbers: "+myOdd);

To append Odd to Even - Just in case you want that
myEven.push.apply(myEven, myOdd);
 
Share this answer
 
Comments
Richard Deeming 10-Aug-21 3:48am    
If you don't want to include 0 in the result, then why start your loop at 0?

On the other hand, why would you not include 0 - which is an even number[^] - in the array of even numbers?
Dimiter2011 10-Aug-21 14:37pm    
The loop has to include the first number, that's why is needed.
Richard Deeming 11-Aug-21 3:03am    
The question makes no mention of that. The assignment doesn't even require that the solution includes a loop, let alone impose any restrictions on the range of the loop variable.

It looks like you're answering a totally different question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900