Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda