Reputation: 25
it is my first question contribution here so i hope it will be ok with all the rules. It is a simple question, i have basic knowledge of language, i have to change values every array[2n] when someone is enterying a value it will multiply the value they entered by the values in the array.
so : tableau[1,2,3,4,5,..,100] must become : if user enter 4 tableau[4,2,12,4,20..]
it is :
tableau = new Array(100);
var userInput = prompt('give me a value');
for (i = 0; i < tableau.lengt; i++) {
tableau[i] = i + 1;
if (i = 0) {
tableau[i] = i * userInput;
console.log('voici mon tableau : [' + i + '] : ' + tableau[i]);
var pair = i % 2;
if (pair = 0) {
tableau[i + 1] = tableau[i] * userInput;
console.log('voici mon tableau : [' + i + '] : ' + tableau[i]);
} else {
console.log('voici mon tableau : [' + i + '] : ' + tableau[i]);
}
}
}
I had issue to implement my code, if you can give me some tips to be able to have a full block of it, i read the help but wasn't able to fix it :(.
So here is my issue, with this code i have an infinite loop, and i am not able to see what is happening because my browser crash. I hope it is not a dumb question and as loop are different for everyone i couldn't find my solution. thank you in advance
Upvotes: 1
Views: 62
Reputation: 12864
There is different trouble on your code.
I made some correction, see live example below
tableau = new Array(100);
var userInput = prompt('give me a value');
for (var i = 0; i < tableau.length; ++i) {
var pair = i % 2;
if (pair == 0) {
tableau[i] = (i + 1) * userInput;
} else {
tableau[i] = i + 1;
}
}
console.log(tableau)
Upvotes: 1
Reputation: 12152
= is used for assignment and == || === are used for comparison. In your if statement either use == or === . = will not work as it will always return true and hence the infinite loop. This will be-
if (i = 0) {
//tableau[i] = i * userInput;
//console.log('voici mon tableau : [' + i + '] : ' + tableau[i]);
//var pair = i % 2;
if (pair = 0) {
this
if (i == 0) {
//tableau[i] = i * userInput;
//console.log('voici mon tableau : [' + i + '] : ' + tableau[i]);
//var pair = i % 2;
if (pair == 0) {
Upvotes: 0
Reputation: 303
You're using assignment operator '=' for comparison here. It should be
if (i == 0)
instead. Otherwise, what you're doing is to set i to 0 every time, so the loop never ends, beause i only have the values 1 (at the end of the loop) and 0 again. Same for
if (pair == 0) {
as well, of course.
Btw., a good practice to overcome this issue is to write
0 == i
instead. This way, if you forget the '=' there won't be any assignment to i happening (you will get an error message).
Upvotes: 1