I'm busy with a udacity excercise and the following question:
A while loop that:
Loop through the numbers 1 to 20
- If the number is divisible by 3, print "Julia"
- If the number is divisible by 5, print "James"
- If the number is divisible by 3 and 5, print "JuliaJames"
- If the number is not divisible by 3 or 5, print the number
I keep submitting the answer but it tells me that my while loop condition is incorrect, Is there anything im doing wrong?
var x = 1;
while (x <= 20) {
if (x/3 === 0) {
console.log("julia" );
} // check divisibility
else if (x/5 === 0) {
console.log("james");
}
else if (x/5 === 0 && x/3 === 0 ) {
console.log("juiliajames");
} // print Julia, James, or JuliaJames
else {
console.log(x);
}
x= x + 1;// increment x
}
You need to use
Modulus (%)instead ofdivide (/). And makex % 5 === 0 && x % 3 === 0as your first condition.Change your code like following.