Reputation: 21
I need to create a HTML that I can type a degree,and click the button, the result of its sin shows, and I know that the Math.sin function will see the input value as radian ,so I add a line that multiply the input value *Math.PI/180,but it suddenly doesn't work, which shows "NaN". I will post these two version of my work. Please somebody help me to find what the problem is.This is the version of radian This is the version of degree, which is the way that I want to try,and the coding is down below
function convert() {
var deg = document.getElementById("an");
var ang = deg * Math.PI / 180;
var si = Math.sin(ang.value);
console.log(si);
sc.innerHTML += si
}
<input type="text" id="an" />
<input type="button" id="sub" value="click" onclick="convert()" />
<div id="sc">
</div>
Upvotes: 1
Views: 169
Reputation: 96
In this line.
var si = Math.sin(ang.value);
you are using ang.value
, which is undefine, and Math.sin(ang.value)
i.e Math.sin(undefined)
giving you NaN.
Try removing .value
and it will work.
var si = Math.sin(ang);
<script>
function convert(){
var deg = document.getElementById("an").value;
var ang = deg* Math.PI/180;
var si = Math.sin(ang);
sc.innerHTML += si
}
</script>
<body>
<input type="text" id="an"/>
<input type="button" id="sub" value="click" onclick="convert()"/>
<div id="sc">
</div>
</body>
Upvotes: 1
Reputation: 11
You should extract value from ## Input ## using the value attribute. And pass it as a Number.
input
<!DOCTYPE html>
<html>
<body>
<input type="text" id="an"/>
<input type="button" id="sub" value="click" onclick="convert()"/>
<div id="sc">
</div>
<script>
function convert(){
var deg = document.getElementById("an").value || 0;
var ang = Number(deg) * Math.PI/180;
var si = Math.sin(ang);
sc.innerHTML += si}
</script>
</body>
</html>
Upvotes: 1
Reputation: 2184
< script > function convert ()
{
var deg = document.getElementById ("an").value;
// var sc = document.getElementById("sc");
var ang = parseInt (deg) * Math.PI / 180;
var si = Math.sin (ang);
sc.innerHTML += si}
</script >
<body >
<input type = "text" id = "an" / ><input type = "button" id = "sub" value =
"click" onclick = "convert()" / ><div id = "sc" > </div > </body >
Upvotes: 1
Reputation: 4005
You are calling input
, not value inside input
And ang.value
is wrong Syntex because ang
is not input element:
<script>
function convert(){
var deg = document.getElementById("an").value;
var ang = deg * (Math.PI/180);
var si = Math.sin(ang);
sc.innerHTML += si;}
</script>
<body>
<input type="text" id="an"/>
<input type="button" id="sub" value="click" onclick="convert()"/>
<div id="sc">
</div>
</body>
Upvotes: 1
Reputation: 1
You need to use parseInt function to parse the value to numeric before calculating:
function convert(){
var deg = parseInt(document.querySelector("#an").value) || 0;
var ang = deg* Math.PI/180;
var si = Math.sin(ang);
document.querySelector("#sc").innerText = si;
}
<input type="text" id="an"/>
<input type="button" id="sub" value="click" onclick="convert()"/>
<div id="sc"></div>
Also, you may want to use querySelector instead of getElementById
.
Upvotes: 1
Reputation: 1107
Change
var deg = document.getElementById("an");
and
var si = Math.sin(ang.value);
To
var deg = document.getElementById("an").value;
and
var si = Math.sin(ang);
function convert(){
var deg = document.getElementById("an").value;
var ang = deg* Math.PI/180;
var si = Math.sin(ang);
sc.innerHTML += si}
<body>
<input type="text" id="an"/>
<input type="button" id="sub" value="click" onclick="convert()"/>
<div id="sc">
</div>
</body>
Upvotes: 1