-2

I am trying to solve the Fizz Buzz question with an extra layer.

This is what I have so far. All this works fine but I need one more extra condition.

It's JS Unit testing

For any other input (valid or otherwise) return a string representation of the input

printFizzBuzz = function(input) {

  // Your code goes here
  for (var i = 1; i <= 20; i++) {

    if (i % 3 === 0 && i % 5 === 0) {
      console.log("FizzBuzz");
    } else if (i % 3 === 0) {
      console.log("FizzBuzz");
    } else if (i % 5 === 0) {
      console.log("Buzz");
    } else {
      console.log(i);
    }
  }
};

Thank you!

Word Rearranger
  • 1,298
  • 1
  • 15
  • 22
beast3372
  • 13
  • 1
  • It would be better to use `switch(i)` rather than lots of `else if` statements – David Bradshaw Nov 27 '19 at 21:05
  • 2
    @DavidBradshaw Highly disagree, `switch` is unnecessarily more verbose and error-prone (for newbies who often forget to `break`). Guess it's opinion-based – CertainPerformance Nov 27 '19 at 21:05
  • @CertainPerformance, that is why we have esLint to spot mistakes like that. When I interview candidates I would expect them to be at a level where they can use a `switch` statement – David Bradshaw Nov 27 '19 at 21:08
  • @DavidBradshaw Sure, knowing *how* it can be used is good, since the syntax exists, and knowing syntax is useful, but that doesn't mean that it *should* be used, IMO – CertainPerformance Nov 27 '19 at 21:10
  • Your issue focused: in order to test the function `printFizzBuzz` JS (or, which test-script, test-framework exactly?) needs a `string` as return? The you need something like a [string-builder](https://stackoverflow.com/questions/2087522/does-javascript-have-a-built-in-stringbuilder-class) instead of `console.log`. Then after loop finished you can return the built string. – hc_dev Nov 27 '19 at 21:11
  • @CertainPerformance I posted how I would answer this below for comparison – David Bradshaw Nov 27 '19 at 21:25

3 Answers3

1

You can use .toString() (JS toString) to change the number in string:

function printFizzBuzz(input) {

  // Your code goes here
  for (var i = 1; i <= 20; i++) {

    if (i % 3 === 0 && i % 5 === 0) {
      console.log("FizzBuzz");
    } else if (i % 3 === 0) {
      console.log("FizzBuzz");
    } else if (i % 5 === 0) {
      console.log("Buzz");
    } else {
      console.log(i.toString());
    }
  }
};

printFizzBuzz();
Word Rearranger
  • 1,298
  • 1
  • 15
  • 22
0

If I was the interviewer, I would prefer the answer to look more like this

function printFizzBuzz(input) {

  // Your code goes here

  for (var i = 1; i <= 20; i++) {

    let by3 = i % 3
    let by5 = i % 5

    switch(0) {
      case by3 + by5:
        console.log("FizzBuzz")
        break

      case by3:
        console.log("Fizz")
        break

       case by5:
         console.log("Buzz")
         break

       default:
         console.log(i.toString())
      }
    }
  }
}

printFizzBuzz()
David Bradshaw
  • 10,116
  • 3
  • 33
  • 61
0

Looking at the question, it is not asking for a loop, it is asking for any value passed to the function. So the answer should look more like this

function printFizzBuzz(input) {

  // Your code goes here

  if (typeof input !== ‘number’) {
    return String(input)
  }

  let by3 = input % 3
  let by5 = input % 5

  switch(0) {
    case by3 + by5:
      return "FizzBuzz"

    case by3:
      return "Fizz"

    case by5:
      return "Buzz"

    default:
      return input.toString()
    }
  }
}
David Bradshaw
  • 10,116
  • 3
  • 33
  • 61