0
Can you help me figure out this error
https://code.sololearn.com/WQelFu58r1RL/?ref=app Am trying to multiply a list of array by 2 but am a null output
8 Respuestas
+ 2
Tijan , You changed code in HTML tab not in JS😁
+ 2
num[i] = num[i] * 2;
What you had done is multiply by the variable itself not by items of variable :)
+ 2
You want to multiply every element of array by 2. right?
see this line:
```
num[i] = num * 2;
```
num*2 is resulting into NaN(Not a Number), because you are trying to perform arithmetic operation on non-numeric type.
valid statement will be:
```
num[i] = num[i] * 2;
```
+ 2
🇮🇳Omkar🕉 yeah 😅 thanks man i apprecitate
0
Kuber Acharya thank you but can you explain it better am kinda new to this language
0
🇮🇳Omkar🕉 am still getting the same error
0
i just updated the code
0
Tijan
In your code it should be
num[i] = num[i]*2
but not
num[i] = num*2
-- Suggestion:
You can use Array.map() method for that purpose. It returns an array and you can save that to a new variable.
You can do a whole lot of things, multiply or divide with a constant number and more.
See more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Ex.
--------
let arr = [1,2,3,4,5];
let doubleArr = arr.map( (num) => {
return num*2;
}
// doubleArr = [2,4,6,8,10]
--------