0

I need to write program that will take first input as 'i' and this loop need to go until first input is less or equal to second input. what I had written but seems not working:

function provjeri() {

    var i  = document.getElementById('prvi').value;
    var b = document.getElementById('drugi').value;
    var c = "";

    for (; parseInt(i) <= parseInt(b)  ; i+=2) {
        c+=i ;
    }

    document.getElementById('rez').innerHTML = parseInt(c);
}
5
  • e.g. first input is 3 secound input is 7, output is 3 plus 5 plus 7 equal 15 Commented Apr 13, 2016 at 15:49
  • I don't think you can use a for loop like that. A for loop has a clear syntax which contains the declaration of the index to loop over. You should use a while loop. Additionally you are cancating strings rather than doing mathematics. Your result will be "357" not 15 because you define c as a string and then add a string to it. Commented Apr 13, 2016 at 15:52
  • You can use for loops like that, just don't see it used too often. Commented Apr 13, 2016 at 15:58
  • It is often viewed as more correct to use loops like that because it allows you to keep all your variable declarations in one place, usually the start of the function. Commented Apr 13, 2016 at 16:10
  • One possible issue is that you are using parseInt in your loop declaration for the first 'i' but not the second or the 'i' in the loop body. These will be treated as strings, as will c as it is declared using c="". There could be other problems also. Commented Apr 13, 2016 at 16:16

4 Answers 4

1

parseInt() when getting the value. Also use c=0, c="" is considered as a string and new values are concatinated to c

function provjeri() {
  var i = parseInt(document.getElementById('prvi').value);
  var b = parseInt(document.getElementById('drugi').value);
  var c = 0;
  
  for (; i <= b  ; i+=2) {
    c+=i ;
  }
  document.getElementById('rez').innerHTML = parseInt(c);	
}
<input id='prvi' />
<input id='drugi' />
<button onclick='provjeri()'>Calc</button><br>
<span id='rez'></span>

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

Comments

0

You can do it like this

var k = 0;
var ival = parseInt(i);
var bval = parseInt(v);

for (k = ival; k <= bval; k += 2) {
   c += k;
}

Comments

0

I guess, you are looking for this:

function provjeri() {
    var i = document.getElementById('prvi').value;
    var b = document.getElementById('drugi').value;
    var c = 0; // needs to be a number
    i = parseInt(i, 10);
    b = parseInt(b, 10);

    for (; i <= b; i += 2) {
        c += i;
    }
    document.getElementById('rez').innerHTML = c;
}
<input id='prvi'></input>
<input id='drugi'></input>
<button onclick='provjeri()'>call function</button><br>
<span id='rez'></span>

2 Comments

You also defined c as a string which makes it concat rather than add up. Edit: better now.
@Hotshot 0246 is it desired output, or it's error? just tried, snippet working fine
0

This is how it should look like:

function provjeri() {
    var i  = parseInt(document.getElementById('prvi').value);
    var b = parseInt(document.getElementById('drugi').value);
    var c = 0;
    while(i <= b){
       c += i;
       i += 2;
    }
    document.getElementById('rez').innerHTML = c;
}

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.