List of Numbers

Asked

Viewed 58 times

0

Good afternoon I’m studying Js.

I created a code that shows a list of numbers from 1 to 10 but I do not want my result to show the numbers 2 and 9.

My problem is not knowing how to express this doubt in the research or the name/ term that this my problem fits.

<meta charset="UTF-8">

<script>

    function pulaLinha() {

        document.write("<br>"); 
        document.write("<br>"); 
    }

    function mostra(frase) {

        document.write(frase);
        pulaLinha();
    }

var i = 0
var cont = (i +1);

    while (cont <= 10) {
        mostra (cont++);


}

</script>
  • You really need to use the while, can’t be a for?

  • Good afternoon Pablo. I am studying so on my own I discovered "for" today ( rsrs), so I would like to find a way with while. Grateful

  • what you need is to use the conditional If, Example: If the value is equal to 2 or 9 then do something if you don’t do something else.

2 answers

1

From what I understand it would be just not call the function if it is the number 2 or 9, getting like this:

<meta charset="UTF-8">

<script>

    function pulaLinha() {

        document.write("<br>"); 
        document.write("<br>"); 
    }

    function mostra(frase) {

        document.write(frase);
        pulaLinha();
    }

var i = 0
var cont = (i +1);

      while (cont <= 10) {
    if(cont !== 2 && cont !==9 ){
        mostra (cont);    
    }
    cont++;
}

</script>
  • Only this way you will have an infinite loop, because when the cont reaches 2 it will no longer increase. see how I did https://jsfiddle.net/mkzdn5q1/

  • Thank you very much. This is what I was looking for as I should call it !== , denial ?

  • Gilson answered your question and put some links so you can have a bigger base.

  • You’re right @Pablotondolodevargas edited

  • @Gilsonrodrigo, yes this is denial

0

First think of the logic you need, basically we will get the following idea:

If the counter is equal to 2 or equal to 9 I should not display it

OR

If the counter is different from 2 and different from 9 should display it

Next step is to figure out how to do this in javascript basically you will need to use a conditional structure combined with a comparison operator.

Resulting in that:

if(cont != 2 && cont != 9){ 
   //Se for diferente de 2 e 9
   //Chamar função para exibir cont
}

See the full example applied to your code:

function pulaLinha() {
    document.write("<br>"); 
    document.write("<br>"); 
}

function mostra(frase) {
    document.write(frase);
    pulaLinha();
}

var cont = 1;
while (cont <= 10) {

  if(cont != 2 && cont != 9){ //Se for diferente de 2 e diferente de 9
    mostra (cont);            //Exibo  o contador
  }
  cont++;                     //Sempre incrimento 1 ao contador
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.