How do I add a number to the results of a loop?

1

Hi, I have a decreasing while loop to which I have to add 3 each time I show an even number, but I can not get it added to me. What am I doing wrong?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Descendiente Numeros Pares</title>
</head>
<body>
<script type="text/javascript">

var num = 100;

while(num >= 50){

document.write("Numero: " + num + "<br>");
if(num%2==0){
num + 3;
 }
num--;
}


</script>   
</body>
</html>
    
asked by condor12 04.08.2017 в 23:45
source

1 answer

1

Your script could be:

var num = temp = 100;

while ( num >= 50 ) {



    //Si número par -> añado 3
    if (( temp % 2 ) == 0 ) {
        document.write( "Numero: " + temp + "<br>" );
        temp += 3;
    }

    temp--;
    num--;
}

Modifications:

  
  • Only show the numbers when they are even.

  •   
  • Look at how to add 3 to a variable.

  •   
  • I added another variable, as indicated in the comments, so as not to generate an infinite loop.
  •   
    
answered by 05.08.2017 в 00:03