List of Numbers

0

Good afternoon I'm studying Jesus.

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

My problem is not being able to express this doubt in the search or the name / term that this problem of my fit.

<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>
    
asked by anonymous 06.12.2017 / 20:37

2 answers

1

From what I understand it would just not call the function if it is the number 2 or 9, so:

<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>
    
06.12.2017 / 20:43
0

First of all, think about the logic you need, basically we'll come up with 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 I should display it

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

Resulting from this:

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
}
    
06.12.2017 / 20:56