简体   繁体   中英

Print a string in console.log instead of a number

I want to print the strings: "Fizz", "Buzz" and "FizzBuzz" in console.log instead of the number they refer to. The point is that until now I can get both number and string, but I don't know how to overcome the number value with the string. For example, the console.log of the code below should be 1 2 Fizz and not 1 2 3 Fizz .

 //Define listNumber var listNumber; //Generate numers from 1 to 100 for (i = 1; i <= 100; i++) { listNumber = i; console.log(listNumber); if ((listNumber % 3 == 0) && (listNumber % 5 == 0)) { console.log('FizzBuzz'); } else if (listNumber % 3 == 0) { console.log('Fizz'); } else if (listNumber % 5 == 0) { console.log('Buzz'); } }

You log the initial value before you do your check. Simply move it to the end of the evaluation, so that if it's neither Fizz , Buzz or FizzBuzz it prints the actual number.

 //Define listNumber var listNumber; //Generate numers from 1 to 100 for (i = 1; i <= 100; i++) { listNumber = i; if ((listNumber % 3 == 0) && (listNumber % 5 == 0)) { console.log('FizzBuzz'); } else if (listNumber % 3 == 0) { console.log('Fizz'); } else if (listNumber % 5 == 0) { console.log('Buzz'); } else { console.log(listNumber); } }

Something like this:

 //Define listNumber var listNumber; //Generate numers from 1 to 100 for (i = 1; i <= 100; i++) { listNumber = i; if ((listNumber % 3 == 0) && (listNumber % 5 == 0)) { console.log('FizzBuzz'); } else if (listNumber % 3 == 0) { console.log('Fizz'); } else if (listNumber % 5 == 0) { console.log('Buzz'); } else console.log(listNumber); }

I log listNumber only in case I haven't logged FizzBuzz , Fizz or Buzz .

In the for loop you are printing the current i. If You want the code to work you can add an else statements to it so it will only log once.

var listNumber;

//Generate numers from 1 to 100
for (i = 1; i <= 100; i++) {
  listNumber = i;
  if ((listNumber % 3 == 0) && (listNumber % 5 == 0)) {
   console.log('FizzBuzz');
  } else if (listNumber % 3 == 0) {
   console.log('Fizz');
  } else if (listNumber % 5 == 0) {
   console.log('Buzz');
  }
  else{
   console.log(listNumber);
 }
}

You can use else statement in your conditions structure. In this way numbers will only be printed if all of the conditions are false.

for (i = 1; i <= 100; i++) {
  listNumber = i;

  if ((listNumber % 3 == 0) && (listNumber % 5 == 0)) {
    console.log('FizzBuzz');
  } else if (listNumber % 3 == 0) {
    console.log('Fizz');
  } else if (listNumber % 5 == 0) {
    console.log('Buzz');
  } else {
    console.log(listNumber);
  }
}

I think this is what you are looking. Instead of printing listNumber in for loop every time place an extra else inside of if else clause and print only when its not printed "Fizz(/&)Buzz"

 //Define listNumber var listNumber; //Generate numers from 1 to 100 for (i = 1; i <= 100; i++) { listNumber = i; if ((listNumber % 3 == 0) && (listNumber % 5 == 0)) { console.log('FizzBuzz'); } else if (listNumber % 3 == 0) { console.log('Fizz'); } else if (listNumber % 5 == 0) { console.log('Buzz'); } else { console.log(listNumber); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM