3

So my problem is that I wrote this code that takes two numbers and writes all numbers between those numbers (including the given two numbers). The code works for the numbers 4 and 7, it prints out "4 5 6 7" but when the 2nd number is a little bigger than the first number for example for the numbers 6 and 14 the code does nothing. If someone could explain to me why it is like this and how could I solve this problem I would-be so happy.

function calculate() {
  var start = document.getElementById("number1").value;
  var end = document.getElementById("number2").value;
  var answer = "";
  for (var i = start; i <= end; i++) {
    answer = answer + i + " ";
  }
  document.getElementById("answer2").innerHTML = answer;
}
<input type="number" id="number1">
<input type="number" id="number2">
<button onclick="calculate()">Pokaż</button>
<div id="answer2"></div>

2 Answers 2

5

The value property returns a string representing the value of the value attribute of a text field.
To compare start and end you need to convert them into numbers (e.g. with the Number primitive wrapper), else the string 6 will be (alphabetically) greater than the string 14.

function calculate() {
  var start = Number(document.getElementById("number1").value);
  var end = Number(document.getElementById("number2").value);
  var answer = "";
  for (var i = start; i <= end; i++) {
    answer = answer + i + " ";
  }
  document.getElementById("answer2").innerHTML = answer;
}
<input type="number" id="number1">
<input type="number" id="number2">
<button onclick="calculate()">Pokaż</button>
<div id="answer2"></div>

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

4 Comments

This solved the problem but can you explain why?
@DoğuşHamdi I explained why.
@DoğuşHamdi The string 6 is larger than the string 14, when compared alphabetically, so your for() loop doesn't do anything. Compare: console.log('6' > '14') (true) and console.log(6 > 14) (false)
Oh sorry didn't see it, thank you for helping :)
2

That's the alternative solution with parseInt in for

function calculate() {
  var start = document.getElementById("number1").value;
  var end = document.getElementById("number2").value;
  var answer = "";
  for (var i = parseInt(start); i <= parseInt(end); i++) {
    answer = answer + i + " ";
  }
  document.getElementById("answer2").innerHTML = answer;
}
<input type="number" id="number1">
<input type="number" id="number2">
<button onclick="calculate()">Pokaż</button>
<div id="answer2"></div>

1 Comment

But why would you want to re-parse end for every value of i? I think it would make more sense to do it as part of assigning it to end. Also you should maybe mention that the input accepts hexadecimal (and potentially octal, depending on the browser/runtime) "numbers" this way, since you don't pass a second parameter: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

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.