mod geek mod geek - 6 months ago 21
Javascript Question

How To Solve Minus Value In Calculator Using jQuery

I am trying to solve minus problem .

For example -8 -4 =-12
But I want to show the number of steps

for example -8 -9 -10 -11 -12


I want to solve problems like
-8 + 4 = - 4
and would like to display the number of steps between
-8 to +4 for example -8 -7 -6 -5 -4
through loop

through JavaScript loop

I have taken two textbox for value one and two . now i am iterating through javascript loop but it is not working

$("#submitBtn").click(function(){

var x;
var minus;
var textbox1=$("#value1").val();
var textbox2=$("#value2").val();


minus=textbox1-textbox2;

for(x=textbox1;x>=minus;x--){
alert(x);
}

});


this is the html

<input type="textbox" id="value1"/>
<input type="textbox" id="value2"/>
<input type="submit" id="submitBtn" value="submit" name="submit"/>

Answer

Try this

$("#submitBtn").click(function(){

    var x;
    var minus;
    var textbox1=$("#value1").val();
    var textbox2=$("#value2").val();
    minus=textbox1-textbox2;
    if(textbox1 >= minus){
      for(x=textbox1;x>=minus;x--){
        alert(x);
      }
    }else{
      for(x=textbox1;x<=minus;x++){
      alert(x);
      }
   }

});

jsfiddle