1
var randNum = function(){
var max = document.getElementById('max').value;
var min = document.getElementById('min').value;
var reps = document.getElementById('reps').value;
var i;
for (i=0; i<reps; i++){
    var num = min + Math.floor(Math.random() * (max - min + 1)); //rand # 0-(max-1)
    addTXT(num);
}
};

This function is supposed to take the numbers typed into the text boxes with the IDs 'min', 'max', and 'reps', and use them to come up with some random numbers. It doesn't write anything to the HTML document. The addTXT() function should write the number to a div, and I know it works because I've used it just fine before this. Any help would be appreciated. Also, I've been working with javascript less than a week, so I'm suspicious the problem is really obvious and I just don't see it.

var addTXT = function(newText){
var outputDiv=document.getElementById("console");
var oldText = outputDiv.innerHTML;
outputDiv.innerHTML = newText + oldText;
};

2 Answers 2

1

Parse the string values into integers with parseInt():

var max = parseInt(document.getElementById('max').value);
var min = parseInt(document.getElementById('min').value);
var reps = parseInt(document.getElementById('reps').value);

DEMO.

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

7 Comments

I declared other number variables like this without parseInt which worked just fine. I did implement this just in case there's more than one issue. No change. Thanks, though, that could be useful in something else I've been working on. :)
@TwoRoses: Can you compare your code with my demo? It's working fine for me. So it must be something that you haven't included in your question.
Hosted here: freetexthost.com/ovgggch6sb is the entire script portion of my page. Unfortunately other things have started to get buggy now too. My main problem is finding a way to use three different if statements to fix errors, but not being able to use else statements and have all of them apply. I've commented out the error checking stuff to keep it working. Maybe I should have started with something simpler...
And where are you actually calling randNum()?
In the HTML. <input type="button" class="btn" onclick="randNum();" value="Random #"/><br/>
|
0

Where do you get the issue? If you unsure, try to put an alert in on certain spots in your code.

For example:

var randNum = function(){
alert('Function is triggered');
var max = document.getElementById('max').value;
var min = document.getElementById('min').value;
var reps = document.getElementById('reps').value;
var i;
for (i=0; i<reps; i++){
    var num = min + Math.floor(Math.random() * (max - min + 1)); //rand # 0-(max-1)
    alert(num);
    addTXT(num);
}
};

If your addTxt function does not work, try the jQuery way

var randNum = function(){
alert('Function is triggered');
var max = document.getElementById('max').value;
var min = document.getElementById('min').value;
var reps = document.getElementById('reps').value;
var i;
for (i=0; i<reps; i++){
    var num = min + Math.floor(Math.random() * (max - min + 1)); //rand # 0-(max-1)
    alert(num);
    $('#console').append(num);
}
};

2 Comments

I tried an alert within the loop and one after declaring 'var i;' to check the other three variables. No alert boxes or text shows up with or without using parseInt in declaring them.
jQuery also does not work. At least not using the Google API linked version. I don't know if that makes any difference. I'd like to keep everything in the one file though, best I can.

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.