Paired number stack javascript

0

I'm trying to pull the odd numbers out of a stack in an expression while javascript but I can not, someone would have a tip to help me.

Follow the code below:

var j=0, msg="";
while (j<=10){
     if(j ==10){
       msg +=j;
       break;
     }
     if(j%2 !=0){
       msg += "";
     }
    msg += j + ", ";
    j++;
  };
console.log(msg);

console.log() shows me the odd numbers too but I want only the pairs.

    
asked by anonymous 27.10.2017 / 01:33

4 answers

0

The problem is that concatenation is always done because it is outside of if :

if(j%2 !=0){ //e devia ser ==0 para ser pares
    msg += ""; //devia estar a juntar aqui o numero aqui
}
msg += j + ", "; //e não aqui, que irá acontecer para todos

It should then look like this:

if(j%2 == 0){
    msg += j + ", ";
}

As a matter of grace and the style of Codegolf, I show another (less educational) way of doing the same:

let msg = [...Array(11).keys()].filter(x => x%2==0).map(x => ""+x).join(',');

console.log(msg);
    
27.10.2017 / 01:59
0

I do not understand why compare with 10, the loop already understands that the 10 is included. The msg += j + ", "; line should be within the if conditional. Finally, break is not necessary in this case.

Follow the code:

var j = 0, msg = "";
while (j <= 10) {
    if (j % 2 === 0) {
        msg += j + ", ";
    }
    j++;
}
console.log(msg);

EDIT

Comparing with 10 to end with .

var j = 0, msg = "";
while (j <= 10) {
    if (j % 2 === 0) {
        if (j === 10){
            msg += j + ". ";
        }else{
            msg += j + ", ";
        }
    }
    j++;
}
console.log(msg);
    
27.10.2017 / 01:47
0

I hope you help;

<script type="text/javascript">
    var j=0, pares="", impares="";
    while (j<=10){
        //Concatena os números pares
        pares   +=  (j%2 == 0 )?j+",":"";
        //Concatena os números impares
        impares += !(j%2 == 0 )?j+",":"";
        j++;
     };
     //Remove o último caracter, no caso, a virgula final
     pares = pares.substr(0,(pares.length - 1)); 
     impares = impares.substr(0,(impares.length - 1)); 

    console.log(pares);
    console.log(impares);
</script>
    
27.10.2017 / 02:58
0

In principle I will correct your logic, and at the end of the answer a leaner script.

In your code, when j is odd it enters the condition if (when it is odd) and it does not concatenate anything, the script proceeds to the bottom line that is concatenating all values of j.

  //se for impar
  if(j%2 !=0){
     //não concatena
     msg += "";
   }
   //sai do if e executa a linha abaixo, sendo impar ou par
   msg += j + ", ";'
  

Then you have to put a else in your script, so if it is odd it goes into IF and it does not execute ELSE and if it does it does not execute IF and execute ELSE p>

if(j%2 !=0){ msg += ""; }else{ msg += j + ", "; }

See the result

var j=0, msg="";
while (j<=10){
     if(j ==10){
       msg +=j;
       break;
     }
     if(j%2 !=0){
       msg += "";
     }else{
       msg += j + ", ";
    }
    j++;
  };
console.log(msg);

The same result looks like this:

var j=0, msg="";
while (j<=10){
     
     //só concatena se forem números pares
     if(j%2 ==0){
        msg += j + ", ";
     }
    j++;
  };
//retira ultima virgula com ultimo espaço 
msg = msg.substr(0,(msg.length -2));
console.log(msg);
    
27.10.2017 / 01:58