Hello guy! I have a problem with array, i don't know how to build a new array using the numbers of other array!
EX: I have
array1 = [1, 4, 2, 13, 5]
with this array1 I want to build other array2 using the numbers in the first array. So i don't know how to take number from array and put it in an other array!
Thanks all
But if I want put in array2 only the even or odd number???
How to do???
What does that mean? Do you mean only the odd/even indices (1,3,5 etc. or 0,2,4 etc.) or do you mean populate the second array only with numbers in the first array which are odd/even? (1,13,5 are odd in your example).
Surely you can guess that this would involve an if...else statement.
if (array1[i]%2 == 0) // value is even
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Yes using your first method, im solve alone but now i have an other problem
All code is write right but, remainder is NaN and evenOdd don't have value of "i"
Code:
function exchangeSymmetric ()
{
var arrayPrincipal = [4, 2, 13, 0, 5];
var array1 = [];
var array2 = [];
for (i = 0; i < arrayPrincipal.length; i++)
{
var evenOdd = array1 [i];
var remainder = evenOdd%2;
}
alert (remainder);
}
Yes using your first method, im solve alone but now i have an other problem
All code is write right but, remainder is NaN and evenOdd don't have value of "i"
Code:
function exchangeSymmetric ()
{
var arrayPrincipal = [4, 2, 13, 0, 5];
var array1 = [];
var array2 = [];
for (i = 0; i < arrayPrincipal.length; i++)
{
var evenOdd = array1 [i];
var remainder = evenOdd%2;
}
alert (remainder);
}
Your code makes no sense to me.
You create an arrayPrinciple and use the length in a for...loop,
but you never check its contents.
When you make an assignment to the evenOdd variable,
you are using an element position in an empty array1 ... hence the NaN error.
You save the last remainder of the tests which is what becomes alerted,
but it is outside the for...loop which is why only one alert occurs.
What is it that you are really trying to accomplish?
Hello guy! I have a problem with array, i don't know how to build a new array using the numbers of other array!
EX: I have
array1 = [1, 4, 2, 13, 5]
with this array1 I want to build other array2 using the numbers in the first array. So i don't know how to take number from array and put it in an other array!
Thanks all
A simple way to make a copy of an array without needing a loop.