While loop output not correct

215 views Asked by At

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
}

5

There are 5 answers

0
PSK On BEST ANSWER

You need to use Modulus (%) instead of divide (/). And make x % 5 === 0 && x % 3 === 0 as your first condition.

Change your code like following.

var x = 1;

while (x <= 20) {

  if (x % 5 === 0 && x % 3 === 0) {
    console.log("juiliajames");
  } // print Julia, James, or JuliaJames
  else if (x % 3 === 0) {
    console.log("julia");
  } // check divisibility
  else if (x % 5 === 0) {
    console.log("james");
  } else {
    console.log(x);
  }
  x = x + 1; // increment x
}

0
Kumar Aman On
var x = 1;

while (x <= 20) {
if(x%3 === 0 && x%5 === 0 ){
console.log("juiliajames" );
}                        // check divisibility
else if(x%3 === 0){
console.log("juilia");
}
else if (x%5 === 0){
console.log("james");
}                           // print Julia, James, or JuliaJames
else{
console.log(x); 
}
x= x + 1;// increment x
}

If you want to check divisibility, you should use the % operator instead of / operator.

0
Hearner On
  • Check x divisible by 5 AND 3 at the begining or it will never been done because if it is divisible by 3 your loop won't go to the else if statment.
  • To check divisibility use modulo (x/3 === 0 only for x = 0)

var x = 1;

while (x <= 20) {
    if(x%5 === 0 && x%3 === 0){
    console.log("juiliajames" );
    }                        
    else if(x%5 === 0){
    console.log("james");
    }
    else if (x%3 === 0 ){
    console.log("juilia");
    }                           
    else{
    console.log(x); 
    }
    x= x + 1;// increment x
}

0
Mr. Reddy On

My approach works by setting bitwise flags on an integer. If it's divisible by three (value % 3 === 0, where % is 'modulo' which gives an integer remainder from division) The first bit is set and if it's divisible by five the second bit is set. That gives a result that could have three binary values 01, 10 or 11, or in decimal 1, 2 & 3 (The three comes about when both bits are set).

var DIVISABLE_BY_THREE = 1;
var DIVISABLE_BY_FIVE = 2;
var DIVISABLE_BY_THREE_AND_FIVE = 3;
var value = 0;

while(value++ < 20) {
 var modulo_3 =  (value % 3 === 0) | 0;
 var modulo_5 = ((value % 5 === 0) | 0) << 1;
 
 switch(modulo_3 | modulo_5) {
  case DIVISABLE_BY_THREE:
   console.log("Julia");
  break;
  
  case DIVISABLE_BY_FIVE:
   console.log("James");
  break;
  
  case DIVISABLE_BY_THREE_AND_FIVE:
   console.log("JuliaJames");
  break;
  
  default:
   console.log(value);
  break;
 }
}

0
mbadeveloper On

var x = 1;

while (x <= 20) {
  var name ="";
  
  if (x % 3 == 0) {
    name = name + "julia";
  } 
  
  if (x % 5 == 0) {
    name = name + "james";
  }
  
  if(name.length > 0)
 console.log(name);  
  else
 console.log(x);
  x++;
}