I have a codewars problem below:
Given an array of 4 integers [a,b,c,d] representing two points (a, b) and (c, d), return a string representation of the slope of the line joining these two points.
For an undefined slope (division by 0), return undefined . Note that the "undefined" is case-sensitive.
a:x1 b:y1 c:x2 d:y2
Assume that [a,b,c,d] and the answer are all integers (no floating numbers!). Slope: https://en.wikipedia.org/wiki/Slope*
I can get the slope for any numbers when 0 is not in the denominator (bottom of the fraction). However, whenever 0 is in the denominator, I get this message:
*expected undefined to equal '0'
I'm not sure on how to solve this part. My code so far is below:
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';
}
}
}
Thank you!
This is happening probably because you are not passing values.
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));
}
You are returning 'undefined', not undefined. Change this and then it should work.