1

I have a simple program that takes in an input and then should show that input multiplied up to 100. I am new to this, but have tried to get it to work before posting here. I have the link to the program that I am referring to.

I want the result to be shown, but I cannot figure out why it is not showing.

You can see what I have below. I think I do not have the html and javascript hooked up properly.

Here is my html:

<body>
  <input type="number" id="NumToBMultiplied" class="value" placeholder="Enter an integer" />
  <input type='submit' id='RunProg' class='button' />
  <p> id='result'</p>
</body>    

Here is my Javascript:

h = document.getElementByID('NumToBMultiplied');
var result = document.getElementbyID('RunProg').addEventListener('click', function () {
    for (i = 0; i < 100; i++)
        return i * h;
    }
});
document.getElementByID('result').innerHTML = result;

http://jsbin.com/wayejequxu/1/edit?html,js,output

Any help is appreciated!

4
  • You should add the code to the question instead of just a link. Commented Apr 19, 2015 at 18:34
  • getElementByID is incorrect, use: getElementById lower case d instead Commented Apr 19, 2015 at 18:43
  • I'm sorry but I'm a bit confused with your question, what's the desired output ? do you really want to multiply h x i 100 times ? Commented Apr 19, 2015 at 18:59
  • Well, the number (100) is arbitrary. I am working through a few code challenges trying to learn Javascript. I just picked 100, but I realize 5 would work as well. Commented Apr 19, 2015 at 19:23

4 Answers 4

2

From how I understand your code, you are wanting to multiply the input 100 times, then output that into a HTML tag. The result in your example is not being added to the result paragraph as it isn't in the loop.

HTML

This is changed only slightly. Notice the onClick="solve()" to the button instead of adding an event listener.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<input type="number" id="NumToBMultiplied" class="value" placeholder="Enter an integer"/>
    <input type="submit" id="RunProg" onClick="solve()" class="button"/>
    <p id="result">&nbsp;</p>
</body>
</html>

Javascript

I've added a line break after each result of the for loop so the result is easier to read. And the output is cleared before a new solve() is run.

var output = document.getElementById("result");

function solve() {

  var input = document.getElementById("NumToBMultiplied").value;

  output.innerHTML = "";

  for(i=0; i < 100; i++) {

    output.innerHTML += i * input + "<br/>";

  }

}

Result here: http://jsbin.com/foduyofewi/1/

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

1 Comment

Yes! This is what I was trying to do. Now that I see a working solution maybe I can get one to work with an event listener. Thanks, I appreciate your help!
1

You need to store your result in variable inside a callback and set innerHTML also in callback:

document.getElementById('RunProg').addEventListener("click", function() {
    var result = 1;
    var input = +h.value;
    for (var i = 1; i < 100; i++) {
        result *= i * input;
    }
    document.getElementById("result").innerHTML = result;
});

DEMO

2 Comments

I cannot run your code successfully, I get: ReferenceError: h is not defined, you may have to initialize var h
When I run your solution it gives one answer. I was wanting it to show a list. For example if you put 1 in, then you would get 1x1, 1x2, etc. To me it seems like a for loop should do that. Am I missing something? I think a while loop would work, but should it also work with a for loop?
0

Pure Javascript version:

function multiply(x) {
	var result = document.getElementById('result');
    result.innerHTML = x.value * 100;
}
<input type="number" id="NumToBMultiplied" class="value" onchange="multiply(this)" placeholder="Enter an integer"/>
<p id="result"></p>

This is the jQuery version:

$(document).ready(function() {
    $('#NumToBMultiplied').on('change',function(){
      $('#result').text($(this).val()*100);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="NumToBMultiplied" class="value" placeholder="Enter an integer"/>
<p id="result"></p>

6 Comments

Is jQuery being used ?
You may be talking about a different question, not this one.
Callie asked this: I have a simple program that takes in an input and then should show that input multiplied up to 100. I gave him/her a proper solution.
I am actually trying to avoid jQuery. I am trying to really learn Javascript so I wanted to try and use something without jQuery.
Coming in a moment :)
|
0

Just some modification to your code to remove some of the typos and errors. I'm not sure you can return the result like how you have done it, a more traditional approach is shown below.

Full code

<!DOCTYPE html>
<html>
<head>
</head>
<body>
 <input type="number" id="NumToBMultiplied" class="value" placeholder="Enter an integer" />
  <input type='submit' id='RunProg' class='button' />
  <p id='result'> </p>
</body>

<script>
h = document.getElementById('NumToBMultiplied').value;
document.getElementById('RunProg').addEventListener('click', function () {
    for (i = 0; i < 100; i++){
        var  result = h*i;
        }
    document.getElementById('result').innerHTML = result;
});


</script>

</html>

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.