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

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

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Suggested learning programs
Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months
Layout Designer icon
Profession
Under development beginner
Layout with the latest CSS standards
start anytime 5 months