Need explain

pls can any body explain to me why this didn’t work?

Hi @payiyke and welcome to the community :wave:

There isn’t any code in your post. The forum removes a lot of code when just pasted into it. You need to write three backtick in the line above and below your code to make it visible:

```
your code
```

It would probably be good to also explain a bit more what’s the problem.

See you,
Michael

Thanks for pointing that out @mikoMK
why can’t i declare the createElement outside of the scope of the for loop?

<script>
    const myArray = ['tomatoes', 'chick peas', 'onions', 'rice', 'black beans'];
    const list = document.createElement('ul');
	const item = document.createElement('li')
    // Add your code 
	for(i = 0; i < myArray.length-1; i++) {
		item.textContent = myArray[i];
		list.appendChild(item);
		
	}
	
	
    // Don't edit the code below here!

    const section = document.querySelector('section');
    section.appendChild(list);
  </script>
1 Like

Thanks for the details!

We want every item of our array to be its own list item. Therefore we need to put the every text (e. g. tomatoes) into its own <li> (e. g. <li>tomatoes</li>) before attaching it to the <ul>.

Does that make it clearer?

thanks for response, are you saying that declaring the variable item inside the scope of the for loop allows the list items to come into play only when the loop runs?

1 Like

Yes, every round of the loop a new <li> gets generated and then attached to the <ul> at the end of the loop. In this example there will be five <li> created (one for every array element). Before the loop runs there is only an empty <ul></ul>.

oh ok, I get it better now, thanks a lot, and Thanks for MDN

1 Like

are u available for another quick one? i don’t really get the break statement as regards loops, can you please explain break to me in this instance when commented out and uncommented out?

const name = 'Mustafa';

const phonebook = [
      { name : 'Chris', number : '1549' },
      { name : 'Li Kang', number : '9634' },
      { name : 'Anne', number : '9065' },
      { name : 'Francesca', number : '3001' },
      { name : 'Mustafa', number : '6888' },
      { name : 'Tina', number : '4312' },
      { name : 'Bert', number : '7780' },
      { name : 'Jada', number : '2282' },
    ]

    // Add your code here
	for(const contact of phonebook){
         if(name === contact.name)
//              break;
		console.log(contact.name, contact.number)
		
	}
~~~

Sure. :slightly_smiling_face:

break causes the code to exit the loop.
So in this example when break is used the first four names are logged. When it reaches “Mustafa” on the fifth round the break command is issued and the loop is exited.

I also recommend using curly braces with if statements. Without curly braces, only the first (uncommented) line after if is inside. This can be confusing

if(name === contact.name)
// break;
console.log(contact.name, contact.number)

means

if(name === contact.name) {
  console.log(contact.name, contact.number)
}

and

if(name === contact.name)
break;
console.log(contact.name, contact.number)

means

if(name === contact.name) {
  break;
}
console.log(contact.name, contact.number)

thanks lot, would have asked for help about his one generally, but i like the way you articulate your response. so pls if you can help me with this final instance.

  • all i m trying to do is to log the prime numbers only to the console.

  • but I m either getting an infinite loop or logging all the numbers instead of the prime number to the console.

  • also the placement of the continue statement confuses me here, as well as the placement of the final expression.

pls assist me with bot a while loop and a do-while loop of this instance. thanks again.

let i = 500;
//    const para = document.createElement('p');

    function isPrime(num) {
      for(let i = 2; i < num; i++) {
        if(num % i === 0) {
          return false;
        }
      }

      return true;
    }
    
    while(i >= 2){
         if(isPrime(i) === false){
              console.log(i)
              i--
              continue;
         }
          if (isPrime(i)){
          console.log(i)
         }
          i--
    }
         
              
1 Like

I’ll try my best :sunglasses:

General comments

  • When testing for a false value in a if condition you can use the “not” operator (!) instead of === false: if(!isPrime(i))
  • continue stops the current round of the loop and will start the next round. That’s different to break which also stops the current round, but will also exit the whole loop.
  • Instead of testing something for false and immediately after for true (or vice versa) you can use if...else

Problem of your code

You stated that you want to log the prime numbers. So you don’t want to do anything when the number isn’t prime. Therefore you can simply delete the first if statement and all its content.

while vs. do…while

Since a do..while loop checks the condition after the round, it will always run at least once. For example if we have let i = 1 and the condition is (i >= 2), then a while loop will never run, but a do…while will run once, because it checks the condition after the round.

I hope that makes the whole situation a bit more understandable.

oh yes, thanks, i think the question got me confused a bit, in the question it said “continue to the next…” something like that, so I thought the question meant that “continue” must be introduced somewhere. thanks again

1 Like

Theoretically you could test for “not prime” and continue in this case. But overall that doesn’t seem easier.

By the way: Your email address is visible in your post. It ended up in the optional “name” field of your profile. I recommend removing it.