# Problem of exercise prime numbers

**URL:** <https://forum.codewithmosh.com/t/problem-of-exercise-prime-numbers/7408>\
**Category:** JavaScript\
**Created:** [September 18, 2021, 4:59am UTC](https://forum.codewithmosh.com/t/problem-of-exercise-prime-numbers/7408 "2021-09-18T04:59:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryanb](https://avatars.discourse-cdn.com/v4/letter/r/7ba0ec/32.png) [@Ryanb](https://forum.codewithmosh.com/u/Ryanb)\
**Post date:** [September 18, 2021, 4:59am UTC](https://forum.codewithmosh.com/t/problem-of-exercise-prime-numbers/7408/1 "2021-09-18T04:59:03Z")

</div>

Hi guys,

I can understand what mosh did in the video. I am trying to do it another way but it always has problem. Can anyone tell me where the problem of my code is. thanks

showPrimes(10);

function showPrimes(num) {

for (let number = 2; number \<= num; number++) {

```
let result;

for (let factor = 2; factor < number; factor++) {

  if (number % factor === 0) {

    result = 'NO'
    
} else {
  result = number;
}

```

}  
console.log(result);  
}  
}

the result from the console.log() is always 2,3,4,5,6,7,8,9

---

<div class="post-metadata">

**Author:** ![Ruu](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/ruu/32/9008_2.png) [@Ruu](https://forum.codewithmosh.com/u/Ruu)\
**Post date:** [September 18, 2021, 2:44pm UTC](https://forum.codewithmosh.com/t/problem-of-exercise-prime-numbers/7408/2 "2021-09-18T14:44:08Z")

</div>

From a quick glance, it’s probably because you’re never breaking out of your inner for-loop when result is ‘NO’, instead you continue and overwrite it.

```auto
if (number & factor === 0)
{
  result = 'NO'
  break;
}

```

Then it should probably work. Also in your first outer iteration, your result is going to be undefined because number === factor and it will skip your inside loop.
