Home > Back-end >  Manipulating array elements
Manipulating array elements

Time:11-28

I created an array using a for loop.

str = []
(function  () {
for(i=1; i<30; i  ){
str.push(liczba1*i);
}})();

and up to this point everything works perfectly for me. However, I don't know how to build another array based on this. I know there are two methods of array.from and another for loop from that array.

However, I lack the math skills to grasp it.

m = []
(function  () {
for(i=0; i<30; i  ){
m.push(str[i] str[i 1]);
}})();

To sum up, first array [1,2,3,4,5] second array [3,5, 7, 9, NaN] I want to see second array [3, 6, 10, 15...]

CodePudding user response:

The problem here is that in the second for in the last iteration you are calling str[31] that does not exist and end up being a NaN.

A solution would be doing:

m = []
(function  () {
for(i=0; i<str.length - 1; i  ){
m.push(str[i] str[i 1]);
}})();

CodePudding user response:

correct answer is

r =    r    str[i];

m.push(r)

  • Related