Question Timur A in lesson «The iterative process and how to not be lazy», course «Programming fundamentals»

Timur A

my code is:

var i = 2; while (num%i > 0) { i++; } return i;

and OUTPUT says Run failed! Check and fix errors above!. Sad =(

5 0

R. D.

Hello there!

  1. Why are you using var? We don't cover var in this course and we shouldn't really use it anymore in JavaScript. Use let for variables (see lesson 10 for reference). But this exercise should not include any variables.

  2. You suppose to implement an iterative solution with recursion, as explained in the lesson, without using loops or variables.

3

Timur A

Okay then, now i've done everything just as in example, and I cant figure out what am I doing wrong, but it doesn't work.

const iter = (number, counter) => {

if (number%counter===0) {
  return counter;
}
return iter (number, counter+1);

} return iter (num, 2);

0

Timur A

I've given up and watched solution, i feel like i've been tricked a bit =) There something more in solution, something what was never shown in tests, and that's why my code worked fine on jsbin, but didn't work in this lesson. After I added this in my code it has been passed, but I feel emptiness inside me cause I spend my +3 points for something i couldn't know from task's condition =))

0

R. D.

There something more in solution, something what was never shown in tests Could you please elaborate?

Your solution (the second one) produces "Maximum call stack size exceeded" error. This means there is an infinite sequence of calls. Your recursion never stops when number is 1.

Your solution works for other numbers, but it doesn't include this condition explicitly: 4. if nothing divides the number without remainder, then this number is prime, so its smallest divisor is the number itself. If counter has reached the number (or it's smaller than the number — in case when number is 1), then you should return number.

So, if you add this condition explicitly, something like if (counter > number) { return number; }, then your solution will work for all numbers.

(In the teacher's solution we check for 1/2 of the number, not the whole number, because if there are no divisors found until number/2, then there will be no divisors at all. But this is an optional optimization, you can go all the way to number (not number/2), and the function will work the same way).

Hope it helps.

1

R. D.

I'm sorry you feel emptiness, but, to be honest, you did miss the 4th condition from the problem description. Not blaming here, clearly, I gotta make the description clearer, which I will do. So, thank you!

As for the points, you'll get them back, and more! I'm going to add few challenge exercises to this course soon, they are bonus exercises outside of the lessons. For each exercise, you'll get 3 points. Oh, and they don't have teacher's solution unless you solve it yourself first ;-)

3

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