javascript - How do I double every other element in an array? -
i need output 2 arrays @ end of program.
the first array user inputs. second array should copy elements of first , replace every other element double. user saves "1, 2, 3, 4, 5" first array. second array should have: "1, 4, 3, 8, 5".
here have far.
head tags
function todouble(modarray) { var modified = new array(); for(var = 1; < modarray.length; i+=2) { modified.push(modarray[i] * 2); } return modified; }
body tags
var origarray = new array(); var modarray = new array(); while() //ignore while loop part; working fine { origarray = parseint(prompt("enter number: ")); document.write(origarray); //output 1 modarray.push(origarray); } var modified = todouble(modarray); document.write(modified); //output 2
i'm receiving doubled numbers , not entire array. example, if enter "1, 2, 3, 4, 5", "4, 8" output.
how can fix this? thank in advance!
do this:-
function todouble(modarray) { (var = 1; < modarray.length; i+=2) { modarray[i] *= 2; } return modarray; }
Comments
Post a Comment