+ 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 :)

15th Jun 2019, 5:04 AM
Minerals2016
Minerals2016 - avatar
17 ответов
+ 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.
24th Aug 2019, 3:18 AM
Logomonic Learning
Logomonic Learning - avatar
+ 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).
15th Jun 2019, 12:47 PM
Minerals2016
Minerals2016 - avatar
+ 2
All I want is to increase x by 4 every time it hits the 6th iteration of i
15th Jun 2019, 12:47 PM
Minerals2016
Minerals2016 - avatar
15th Jun 2019, 3:51 PM
Minerals2016
Minerals2016 - avatar
+ 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 :(
23rd Aug 2019, 11:07 PM
Minerals2016
Minerals2016 - avatar
+ 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
24th Aug 2019, 1:10 AM
Minerals2016
Minerals2016 - avatar
+ 2
Should I leave the if( i%6 == 5 ) { x += 4; } ? Monical
24th Aug 2019, 1:12 AM
Minerals2016
Minerals2016 - avatar
+ 2
Hmmm alright, thx for the tips and your time Monical :)
24th Aug 2019, 3:24 AM
Minerals2016
Minerals2016 - avatar
+ 1
You are i = 11111 and x too, you have to make it i = 1(x too) cause you're going out of index
15th Jun 2019, 5:13 AM
nicolas turek
nicolas turek - avatar
+ 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-------------------------------------------------------------------------------------*/
15th Jun 2019, 5:16 AM
nicolas turek
nicolas turek - avatar
+ 1
if( i%6 == 5 ) x += 4
15th Jun 2019, 1:46 PM
nicolas turek
nicolas turek - avatar
+ 1
well... maybe try declare all variables at non bracket arrea
15th Jun 2019, 1:53 PM
nicolas turek
nicolas turek - avatar
+ 1
i don't really get what you really want to do... maybe
15th Jun 2019, 3:53 PM
nicolas turek
nicolas turek - avatar
+ 1
on line 63, x=111111, and will always be 111111 while it is in the loop. is it supposed to be that way?
23rd Aug 2019, 10:57 PM
Logomonic Learning
Logomonic Learning - avatar
0
i++ means i+=1, which will only increment your 111111 to 111112, so perhaps try i+=111111 in the for loop
23rd Aug 2019, 11:19 PM
Logomonic Learning
Logomonic Learning - avatar
0
e.g. for (i=1111;i<6666; i+=1111){//other stuff}
23rd Aug 2019, 11:20 PM
Logomonic Learning
Logomonic Learning - avatar
0
youre welcome Minerals2016
24th Aug 2019, 3:25 AM
Logomonic Learning
Logomonic Learning - avatar