In principle I will correct your logic, and at the end of the reply a more streamlined script.
In your code, when j is odd enters the condition if
(when it is odd) and concatenates nothing, 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 + ", ";`
So you missed putting one else
in your script, so if you’re odd you enter IF
and does not execute the ELSE
and if it is even it does not execute the IF
and executes the ELSE
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 is obtained thus:
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);
Welcome mr.Aureli, if any answer solved your problem, see in this post why accepting an answer is important https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079
– user60252