Question Тимур А in lesson «Iterative process and not being lazy», course «Intro to Programming»

Тимур А

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

Тимур А

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

Тимур А

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

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

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

Sign up or sign in

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