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);