我有一个代码战争的问题如下:
当0不在分母(分数的底部)时,我可以得到任意数字的斜率。但是,每当分母中有0时,我就会得到以下消息:
*预期未定义为“0”
我不知道该怎么解决这个问题。目前为止,我的代码如下:
function slope(points) {
let a = (points[0]);
let b = (points[1]);
let c = (points[2]);
let d = (points[3]);
function findSlope(a,b,c,d) {
if (c-a === 0) {
return 'undefined';
}
else if (c-a !== 0) {
let slope = (d-b)/(c-a);
let answer = slope.toString();
return answer;
}
else {
return 'undefined';
}
}
}
谢谢!
发布于 2022-02-23 13:57:33
这可能是因为您没有传递值。
function slope(points) {
let a = points[0];
let b = points[1];
let c = points[2];
let d = points[3];
function findSlope(a, b, c, d) {
if (c - a === 0) {
return "undefined";
} else if (c - a !== 0) {
let slope = (d - b) / (c - a);
let answer = slope.toString();
return answer;
} else {
return "undefined";
}
}
console.log(findSlope(a, b, c, d));
}
发布于 2022-02-23 13:05:38
您返回的是'undefined'
,而不是undefined
。改变这个,然后它就会起作用。
https://stackoverflow.com/questions/71243990
复制相似问题