0

I want the loop to run until the value of prompt but it's going infinite. Need help. Here is my javascript code: My code

var round = prompt("enter text");
var roundno = round;

var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];
var size = images_arr.length
function myFunction() {
for (var i = 0; i = roundno; i + 1) {
    console.log(i);
    setInterval(function () {
        var x = Math.floor(size * Math.random())
        $('#random').attr('src', images_arr[x]);
    }, 1500);

    setInterval(function () {
        var sound = new Audio("../audio/audio.mp3");
        sound.play();
    }, 3000);

    if (i = roundno) {
        break;
     }
    }
  }

2 Answers 2

2

There are a lot of problems with your code.

var round = prompt("enter text"); // how do you know the user enters a number?
var defaultNumberOfRounds = 1;  // I added this row

// CHANGED THIS IN EDIT 
var roundno = round; // should be: isNaN(Number(round)) ? defaultNumberOfRounds : round

var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];
var size = images_arr.length
function myFunction() {

// CHANGED THIS IN EDIT the conditions within () should be: var i = 0; i < roundno; i++
for (var i = 0; i = roundno; i + 1) {
    console.log(i);

    // all iterations in the loop will execute this at the same time.
    setInterval(function () {
        var x = Math.floor(size * Math.random())
        $('#random').attr('src', images_arr[x]); // JQuery
    }, 1500);

    // all iterations in the loop will execute this at the same time.
    setInterval(function () {
        var sound = new Audio("../audio/audio.mp3");
        sound.play();
    }, 3000);

    if (i = roundno) { // should be i == roundno
        break; // don't need to break it, because your for loop's condition should take care of this
     }
    }
  }
} // ADDED THIS IN EDIT: missing curly bracket

[edit] I added a snippet to show that my code is working. I commented all code within the for loop, and I had to change declaring roundno and the statement within the for loop.

var round = prompt("enter text");
var defaultNumberOfRounds = 1;
var roundno = isNaN(Number(round)) ? defaultNumberOfRounds : round;
var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];
var size = images_arr.length;
console.log(`round: ${round}, roundno: ${roundno}`);
function myFunction() {
  for (var i = 0; i < roundno; i++) {
    console.log(i);

    /*setInterval(function () {
        var x = Math.floor(size * Math.random())
        $('#random').attr('src', images_arr[x]); // JQuery
    }, 1500);

    setInterval(function () {
        var sound = new Audio("../audio/audio.mp3");
        sound.play();
    }, 3000);*/

  }
}

myFunction();

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried doing all those changes but it still doesn't work.
I made a mistake, and also discovered a missing bracket. I change part of the code and also made a snippet.
Thank you so very much for helping me out with this. ;-)
2

You are not incrementing i

Change

i + 1

to one of the following:

  • i++
  • i += 1
  • i = i + 1

Also you need to fix the if statement at the end of the loop. You want to check whether i is equal to roundno or not. Instead of using assignment operator = which will assign the value of roundno to i, use == or === for equality check.

if (i == roundno) {
   break;
}

You have made the same typo in loop condition as well. Change loop condition to i == roundno and remove the if statement at the end of the loop because with loop condition fixed, if statement inside the loop is unnecessary.

for (var i = 0; i == roundno; i++) {
   // code
}

1 Comment

For completeness... i = i + 1 will also suffice, additionally there's a typo later on where the OP is assigning i to a new value. The condition later should be: if (i == roundno). Edit: The condition within the loop definition should also be i == roundno.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.