Jump to content

need help with javascript

WannaBeeComputerGeek

<!DOCTYPE html>
<html>
<body>
<title>
Integer Values
</title>
<form id="num1">
1st Numerical:<input name="fnum" type="text" ><br>
</form>
<br>
<form id="num2">
2nd Numerical:<input name="snum" type="text" ><br>

</form>
<button onclick="rt()">RESET</button>
<button onclick="cmd()">COMMAND</button>


<script>

    function rt(){
    document.getElementById("num1").reset();
    document.getElementById("num2").reset();
    }
    function cmd(){
    var sum,temp1,temp2,x,y,sum,sub,mul,div;
    temp1=document.getElementById("num1");
    temp2=document.getElementById("num2");
    x=temp1.elements["fnum"].value;
    y=temp2.elements["snum"].value;
    sum=(parseInt(x)+parseInt(y));
    sub=(parseInt(x)-parseInt(y));
    document.getElementById("demo").innerHTML="The Sum is "+sum+"<br>";
    document.getElementById("demo").innerHTML+="The Difference is "+sub+"<br>";
    document.getElementById("demo").innerHTML+="The Product is "+mul+"<br>";
    document.getElementById("demo").innerHTML+="The Quotient is "+div+"<br>";
    }
</script>
<p id="demo">
</p>
</body>
</html>

 

 

 

 

 

it doesnt show the output for the sub,mul, and div HELPPPPPPPPPPPPP!!!!!!!!!!!!

Link to comment
Share on other sites

Link to post
Share on other sites

mul doesn't seem to be filled in this code, same for div and I would assume if the 1st and 2nd numerical inputs are left empty, sub wouldn't get filled with a value too.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Oh Gosh, that's a mess, please comment the code and/or explain us what you want to achieve and how.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Gachr said:

Oh Gosh, that's a mess, please comment the code and/or explain us what you want to achieve and how.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live” - John Woods

Main Gaming Rig:

Spoiler

Core i7-4770, Cryorig M9i Cooler, ASUS B85M GAMER, 8GB HyperX Fury Red 2x4GB 1866MHz, KFA2 GTX 970 Infin8 Black Edition "4GB", 1TB Seagate SSHD, 256GB Crucial m4 SSD, 60GB Corsair SSD for Kerbal and game servers, Thermaltake Core V21 Case, EVGA SuperNOVA 650W G2.

Secondary PC:

Spoiler

i5-2500k OCed, Raijintek Themis, Intel Z77GA-70K, 8GB HyperX Genesis in grey, GTX 750 Ti, Gamemax Falcon case.

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, FilipSebik said:

Best java help: Learn C++

Best advice: Don't listen to idiots who don't know Java and Javascript are completely different languages.

 

As far as I know you cannot use += with .innerHTML but seeing as you are printing these 4 lines in a row there is no reason you cant set the HTML in one go:

var html = "The Sum is "+sum+"<br />The Difference is "+sub+"<br />The Product is "+mul+"<br />The Quotient is "+div+"<br />";
document.getElementById("demo").innerHTML= html;

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Brenz said:

Best advice: Don't listen to idiots who don't know Java and Javascript are completely different languages.

 

As far as I know you cannot use += with .innerHTML but seeing as you are printing these 4 lines in a row there is no reason you cant set the HTML in one go:


var html = "The Sum is "+sum+"<br />The Difference is "+sub+"<br />The Product is "+mul+"<br />The Quotient is "+div+"<br />";
document.getElementById("demo").innerHTML= html;

 

Ouch, i misread it and now iam the idiot

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

+= is valid JS syntax: http://www.w3schools.com/js/js_operators.asp at least & works fine on my browser with innerHTML when I view that page.

 

The mul and div variables you declare in the var line but then they're never used until the output lines. I.e. there's no assignment like you've done with the sum or sub variable lines!

 

     x=parseInt(temp1.elements["fnum"].value);
     y=parseInt(temp2.elements["snum"].value);
     sum=(x+y);
     sub=(x-y);
     mul=(x*y);
     div=(x/y);

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×