+ 2
Iterate array and manipulate variable ? [JS]
I have a code which works perfectly except with a very long array and all I need to do is to increase a variable x by 4 every fourth item of the array "arr". The loop was so I don't write too make switch cases. My code can be found here: https://code.sololearn.com/W45XgpH9CAf2/#js I have also labeled the problematic code with comments to make it easier to find (as I don't think I was too specific here). Sorry if this is a sloppy question, haven't slept much and have been working on this the whole day. Thanks for anyone who tries to help me :)
17 Antworten
+ 1
I apologise for my previous suggestion as it wont do much help. the reason I suggested it is because I didnt think you’d want to be manipulating the DOM thousand times as reflected in you if else blocks.
leaving the i%6 part depends entirely on what you want it to do. the x variable is within the forloop so will always be 11111, and it is never even used.
So far, the best way ive found to debug code is to separate it into sections.
1) design (html & css)
2) logic (js)
3) Events(js link to html)
so try to work out stage 2 from a console so that you can concentrate on the logic without worrying too much about the html DOM part.
+ 2
nicolas turek The reason I have this problem is because I want the res variable to equal to I and then return the number on the array, but because arrays can't start at any other number other then one subtracting I by x would make it 0 (because 11111 is the first number I want on the array and i - x = 0). The array I have is the Diceware word list so it starts at 11111 that's why I have it like that (and the reason I didn't put it up on my code). I will put a js fiddle of the whole thing (including array) later (as I'm not home currently).
+ 2
All I want is to increase x by 4 every time it hits the 6th iteration of i
+ 2
nicolas turek u think the last answer to this post might help ?
https://stackoverflow.com/questions/30713250/access-every-other-item-in-an-array-javascript
+ 2
Monical it has to increase by 4 every time the loop hits the 6th iteration. Honestly Idk how to do this anymore so I haven't worked on it :(
+ 2
Monical Alright I will try that. Just need to do it later since I have the original array on my pc and see if it works. Ty for the help
+ 2
Should I leave the if( i%6 == 5 ) {
x += 4;
} ? Monical
+ 2
Hmmm alright, thx for the tips and your time Monical :)
+ 1
You are i = 11111 and x too, you have to make it i = 1(x too) cause you're going out of index
+ 1
/*Problematic area--------------------------------------------------------------------------------------------*/
/*array to pick words from (array is too long to include so I made this one)*/
var arr = ['hellow', 'this', 'is', 'an', 'array', 'for', 'testing', 'purposes'];
/*loop to find array and result (reduces having to write 1000 switch statements)*/
function find () {
/*counter*/
for (i = 1; i < 6; i++) {
var x = 1;
if (i == res1) {
word1.innerHTML = arr[i - x];
}
else if (i == res2) {
word2.innerHTML = arr[i - x];
}
else if (i == res3) {
word3.innerHTML = arr[i - x];
}
else if (i == res4) {
word4.innerHTML = arr[i - x];
}
}
}
/*end of problematic area-------------------------------------------------------------------------------------*/
+ 1
if( i%6 == 5 ) x += 4
+ 1
well... maybe try declare all variables at non bracket arrea
+ 1
i don't really get what you really want to do... maybe
+ 1
on line 63, x=111111, and will always be 111111 while it is in the loop. is it supposed to be that way?
0
i++ means i+=1, which will only increment your 111111 to 111112, so perhaps try i+=111111 in the for loop
0
e.g. for (i=1111;i<6666; i+=1111){//other stuff}
0
youre welcome Minerals2016