mod geek - 1 year ago 60

Javascript Question

I am trying to solve minus problem .

`For example -8 -4 =-12`

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

I want to solve problems like

`-8 + 4 = - 4`

`-8 to +4 for example -8 -7 -6 -5 -4`

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 Source

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);
}
}
```

});