Applying conditions to an Array

2019-08-19 21:55发布

I have a problem applying to return an array after applying a condition.

Here it is,

With a given array: [1, 2, 3]

Condition 1: If it is an Odd, should multiply *2.

Condition 2: If it is an Even, just return it.

Expected_Result: [2, 2, 6]

Here is my approach;

function oddToEven(array) {

  var evens = [array];

  var odds = [array];


 if (array %2 !== 0){

    array *2;

    return odds;

   } else {

     return evens;

  }

  }

  oddToEven(1,2,3); // returns => [1]

I know this is pretty basic, and surely my approach is all wrong, but this is my very first week learning JS, I hope some of you give me a light on this!

Thanks a lot

6条回答
萌系小妹纸
2楼-- · 2019-08-19 22:14

Use .map to transform one array into another - what is returned from each call of the callback function will be the item in the same index in the new array:

const oddToEven = array => array.map(
  num => num % 2 === 1 ? num * 2 : num
);
console.log(oddToEven([1, 2, 3]))

Or, to be more verbose:

function oddToEven(array) {
  return array.map(function(num) {
    if (num % 2 === 1) // Odd
      return num * 2;
    else // Even (or not an integer)
      return num;
  }
}

Of course, this assumes that every item in the original array is an integer.

查看更多
疯言疯语
3楼-- · 2019-08-19 22:15

If you want to pass the array in a function :-

     function oddToEven(array) {
      var i;
      var filalOutput = [];
        for (i=0;i<array.length;i++){
          var x = array [i]
          if(x%2==0)
              filalOutput.push(x);
          else{
              filalOutput.push(x*2);
          }
      }

        return filalOutput
    }
    {
      var array1=[];
      const args = [1, 2, 3];
      array1=oddToEven( args);
      console.log(array1);
    }
查看更多
你好瞎i
4楼-- · 2019-08-19 22:16

When doing [array] you basically wrap the array into another array, you probably don't need [[1, 2, 3]]. To copy an array, use [...array], however, do you really need three arrays? Wouldn't it be enough to go over the passed array, and change it according to the rules? For that we have to go over the indices of the array:

function oddToEven(array) {
  for(let i = 0; i < array.length; i++) {
    //...
  }

  return array;
}

Now inside that loop, you can get the current element with array[i], and also modify it with array[i] = ? ... You can also check if it is even with

     if(array[i] % 2 === 0) {
       // todo
     } else {
       // todo
     }
查看更多
闹够了就滚
5楼-- · 2019-08-19 22:22

You can use a simple forEach() loop for that:

var arr = [1, 2, 3, 4, 5, 6, 7];
var res = [];
arr.forEach(function(val){
  if(val % 2 === 0){
   res.push(val); 
  } else {
   res.push(val*2);
  }
});
console.log(res);

查看更多
手持菜刀,她持情操
6楼-- · 2019-08-19 22:24

In JavaScript we usually use map, filter and reduce to solve this kind of problem in a functional way

function oddToEven(array) {
    return array.map(n => (n % 2 === 1 ? n * 2 : n));
}

Note that map will crate a new array so your original array will remain the same. Usage:

const originalArray = [1,2,3,4];
const convertedArray = oddToEven(originalArray);
// origianlArray is [1,2,3,4]
// convertedArray is [2,2,6,4]
查看更多
何必那么认真
7楼-- · 2019-08-19 22:32

This is how you can modify your own code:

 function oddToEven(arr) {

     return arr.map(function(num){
         return num % 2 === 1 ? num * 2 : num
     });

 }

oddToEven([1,2,3]); // here you need to send the array, not 1, 2, 3
查看更多
登录 后发表回答