Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Find the Slope JavaScript

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!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is happening probably because you are not passing values.

Try this :)

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));
}
about 4 years ago · Juan Pablo Isaza Report

0

You are returning 'undefined', not undefined. Change this and then it should work.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!