I just finished the loops 1 and I kindly ask for it to be marked. Thanks in advance
Can I have this inside an online editor like codepen, please? It will be much easier for me to mark that way.
Thanks!
Yes sure sorry for always sending my codes in pictures. I just opened a codepen account so that won’t repeat itself.
please I will prefer to move on to loops 3 as I am having a hard time with it here is my codepen for it https://codepen.io/segunthecanvas/pen/LYVvaVg
Hi there @segunthecanvas!
Yes, loops 3 is a bit tricky. Let me give you a few clues here:
-
i is already 500, and you want to exit the loop when you reach 0. So the exit condition is not going to be
i>=500
. It will want to be greater than or equal to 0. -
You run
isPrime(i);
inside the loop, which is good, but you need to use it as a test. The function returnstrue
orfalse
to say whether the number passed to it is a prime number or not. So you need to write code to say “if it returns false, then continue. If it returnstrue
, then print i to the paragraph text content”. -
This line is basically right —
para.textContent = isPrime(i).textContent + ',';
— except that you are making the whole of the paragraph’s text content equal to the assigned value each time. Instead, you need to concatenate it on to the end of the text content. How do you do this? -
Also referring to the above, you don’t need to use
isPrime(i)
here. You just want to add i to the text content, if it is a prime number.
Hello @chrisdavidmills I have done according to how you directed me but unfortunately my values still show undefined. https://codepen.io/segunthecanvas/pen/dyoEBNg
Hi @segunthecanvas!
The problem here was that you had your JavaScript inside your HTML panel, inside a <script>
element, but then also inside the JavaScript panel. So you effectively had two sets of the same JS being applied to the page, which was making things go weird.
Then you also had the <script>
tags inside the JavaScript pane, which was causing errors, as HTML is not JS.
When you use codepen, and you put some code inside the JavaScript panel, it will automatically apply this to the HTML in your HTMl panel, behidn the scenes, so you don’t need to worry about putting <script>
elements in.
Now on to the JavaScript code. It was mostly OK, and you were getting close. I ended up updating your code to this:
let para = document.createElement('p');
function isPrime(num) {
for(let i = 2; i < num; i++) {
if(num % i === 0) {
return false;
}
}
return true;
}
// Add your code here
for(let i = 500;i>=0; i--){
if(!isPrime(i)){
continue;
}
para.textContent += i + ',';
}
// Don't edit the code below here!
let section = document.querySelector('section');
section.appendChild(para);
The bits I had to update were as follows:
-
if(isPrime(i)%2 !== 0){
changed to!isPrime(i)
. I’m not sure what you were trying to do here.isPrime()
returnstrue
if the argument passed to it is a prime number, andfalse
if it isn’t. To test whether a value istrue
, you just include the value in the test, soisPrime(i)
on its own would do that. We want to test whetherthe value is false, so we need to negate the test, which is how we ended up with!isPrime(i)
. -
para.textContent += i.textContent + ',';
changed topara.textContent += i + ',';
.i
is just a value — it doesn’t have atextContent
property — it is text content. And you are trying to add this text content to the paragraph element’stextContent
property, which contains the text this element contains inside its opening and closing tags.