2

I am trying to write a solution for a javascript application that takes a number input & depending on that number is how many times it runs a specific function. I am using a text box for input field & a button to process the number of times the user wants the function to run. It is for a game of dice game. User enters how many games he wants to play and clicks button to run the roll_dice function that many times. I'm not sure if I am going the right way with using a for loop to take users numeric input and loop through its value until it ends for example

function games() {
        var num = document.getElementById("inp").vale;
        var i;

        for (i = 0; i < num; i++) {
            roll_dice();
        }
}
1
  • what is vale?.. Commented Mar 29, 2017 at 3:47

4 Answers 4

2

You have a typo. It's .value.

You can convert a string to a number by using * 1.

Something like this:

function games() {

  var num = document.getElementById("inp").value * 1; // Convert the string value a number.
  var i;

  for (i = 0; i < num; i++) {
    roll_dice();
  }
}

function roll_dice() {
  console.log("Test.");
}

var btnRun = document.getElementById("btnRun");
btnRun.onclick = function() {
  games();
};
<input id="inp" type="text" />
<button id="btnRun" type="button">Run</button>

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

4 Comments

I appreciate everyone's helps. Danny, I understood your example best, thank you! Now i am stuck with my roll_dice function. I am missing something to allow it to process and display each roll_dice output. This is what I have
function roll_dice() { var die1 = Math.floor(Math.random()*6) +1; var die2 = Math.floor(Math.random()*6) +1; var sum = die1 + die2; var output = "<br>Your Roll = " + sum; var total = sum; if ((sum==7)||(sum==11)) output = output + "<br>You win!"; else output = output + "<br>You Loose!"; document.getElementById("op").innerHTML = output; }
Oh, could you update your post with this code? I can help you.
Danny, I apologize for the delayed response. I was able to figure it out, I had to put my win variable outside the function and into a global variable. I appreciate your time and help!
0

The value you get from input box is string you need to convert it into int . If you are using int in loop .

var num = parseInt(document.getElementById("inp").value);

Comments

0
   function games() {

    var num = document.getElementById("inp").value;
    var i;

    for (i = 0; i < Number(num); i++) {
        roll_dice();
    }
}

Comments

0
**<label>Game:</label><input type="text" id="input" value="0" />
<button id="btn">Submit</button>
  <script>
 document.getElementById("btn").addEventListener("click",function(){
  var inputVal=document.getElementById("input").value*1;
  rollDice(inputVal);
});

 function rollDice(dice){

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<label>Game:</label><input type="text" id="input" value="0" />
<button id="btn">Submit</button>
  <script>
 document.getElementById("btn").addEventListener("click",function(){
  var inputVal=document.getElementById("input").value*1;
  rollDice(inputVal);

});
    
 function rollDice(dice){
  for(var i =0;i<dice;i++){
    var x = Math.random()*100;
    console.log(x);} 
}
 </script>
</body>
</html>

  for(var i =0;i<dice;i++){
    var x = Math.random()*100;
    console.log(x);} 
}
 </script>**

Comments

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.