2015-02-06 93 views
1

我对Javascript很新,所以请原谅我缺乏知识。 我正在尝试使用Javascript来查找两个数据点(x1,y1)(x2,y2)的斜率。斜率等式为m = y1-y2/x2-x1。这里是我在JS小提琴中迄今为止所做的:http://jsfiddle.net/1wvfz0ku/ 问题是,当我尝试计算点时,只有NaN出现。 就像我说的,我在JS编码方面很新颖,所以我不能看到我在哪里搞乱了。任何帮助都将非常感谢! :-)为什么我的JS功能不能正常工作?

<!DOCTYPE html> 
<html> 
<body> 
<div style="width:250px;height:auto;margin:auto;"> 
<h1>Find the Slope of Two Data Points</h1> 
<p> 
Find the slope of two sets of points: 
</p> 
<var>M</var>=<p id="slope"></p> 
Enter your points below: 
<form id="points"> 
X<sub>1</sub>: <input type="text" id="x1"/><br/> 
X<sub>2</sub>: <input type="text" id="x2"/><br/> 
Y<sub>1</sub>: <input type="text" id="y1"/><br/> 
Y<sub>2</sub>: <input type="text" id="y2"/><br/> 
</form> 
<script> 
function findSlope() { 
//The points// 
var xOne = document.getElementById("x1").value; 
var xTwo = document.getElementById("x2").value; 
var yOne = document.getElementById("y1").value; 
var yTwo = document.getElementById("y2").value; 

//slope equation// 
var op1 = yTwo - yOne; 
var op2 = xTwo - xOne; 
var answer = op1/op2; 
document.getElementById("slope").innerHTML = answer; 
} 
function reset(){ 
document.getElementById("points","slope").reset(); 
} 
</script> 

<button type="submit" onClick="findSlope()">Find the Slope</button> 
<button type="submit" onClick="reset()">Clear Points</button> 
</div> 
</body> 
</html> 
+0

对我来说工作正常:http://jsbin.com/mokoya/1/edit?html,console,output – Aweary 2015-02-06 22:32:33

+0

你在测试什么浏览器? *不应该*重要,但这是真实的世界,如果您使用的是<在此处插入不良浏览器>,您将遇到问题 – Avery 2015-02-06 22:34:01

+0

从2006年开始,我在MacBook Pro上使用旧版Chrome。为什么它不能正常工作... – 2015-02-09 16:45:49

回答

1

works just fine除非xTwo - xTwo0,在这种情况下,你试图通过0,这是在JavaScript中定义为NaN来划分数。


那么说:value元素的属性总是一个字符串。它只是发生在你使用的所有操作(-/)都会隐式地将字符串转换为数字,但是如果你使用了+你确实得到了一些非常奇怪的结果(因为带字符串的+是串联的,而不是增加)。最好使用parseInt(theString, 10)(如果您使用的是十进制)来明确确保您处理的是数字。


实施例允许xTwo - xOne0和解析的输入为十进制:

function findSlope() { 
 
    //The points// 
 
    var xOne = parseInt(document.getElementById("x1").value, 10); 
 
    var xTwo = parseInt(document.getElementById("x2").value, 10); 
 
    var yOne = parseInt(document.getElementById("y1").value, 10); 
 
    var yTwo = parseInt(document.getElementById("y2").value, 10); 
 

 
    //slope equation// 
 
    var op1 = yTwo - yOne; 
 
    var op2 = xTwo - xOne; 
 
    var answer = op2 === 0 ? "flat or whatever" : op1/op2; 
 
    document.getElementById("slope").innerHTML = answer; 
 
} 
 

 
function reset(){ 
 
    document.getElementById("points","slope").reset(); 
 
}
<div style="width:250px;height:auto;margin:auto;"> 
 
<h1>Find the Slope of Two Data Points</h1> 
 
<p> 
 
Find the slope of two sets of points: 
 
</p> 
 
<var>M</var>=<p id="slope"></p> 
 
Enter your points below: 
 
<form id="points"> 
 
X<sub>1</sub>: <input type="text" id="x1"/><br/> 
 
X<sub>2</sub>: <input type="text" id="x2"/><br/> 
 
Y<sub>1</sub>: <input type="text" id="y1"/><br/> 
 
Y<sub>2</sub>: <input type="text" id="y2"/><br/> 
 
</form> 
 
<button type="submit" onClick="findSlope()">Find the Slope</button> 
 
<button type="submit" onClick="reset()">Clear Points</button>