Question Julia Ayubova in lesson «Mutators and for loop», course «Intro to Programming»

Julia Ayubova

Tell me please, why this solution doesn't work?

const isPrime = (num) => {
  for (let i = 2; i <= num && num%i !== 0; i++) {
     if (i === num) {
    return true;
  }
  }
  return false;
};

export default isPrime;

OUTPUT:

 FAIL  __tests__/solution.test.js
  ● prime

    expect(received).toBeTruthy()

    Expected value to be truthy, instead received
      false

      at Object.<anonymous>.test (__tests__/solution.test.js:14:65)
1 0

R. D.

Hey!

Check out what the system says:

 FAIL  __tests__/solution.test.js
  ● prime

    expect(received).toBeTruthy()

    Expected value to be truthy, instead received
      false

      at Object.<anonymous>.test (__tests__/solution.test.js:14:65)

Open tests_/solution.test.js file, line 14. There is a test that describes how Hexlet checks if your solution is correct. At that particular line there is a certain number that should be detected as prime (i.e., your function should return true), but your function returns false.

Then try to replicate how your function works while checking that particular number. Something is wrong with the logic there.

0

Используйте Хекслет по максимуму!

  • Задавайте вопросы по уроку
  • Проверяйте знания в квизах
  • Проходите практику прямо в браузере
  • Отслеживайте свой прогресс

Sign up or sign in

By sending the form, you agree to "Personal Policy" and "Service Conditions".