I get different outputs when I use console.log() in my function vs when I use the return statement.
When I run the function with the return statement I get a one word output which is one of the following: 'fizz' 'buzz' or 'fizzbuzz', but when I run the function using console.log the output is counting to the limit and saying 'fizz' 'buzz' or 'fizzbuzz' whenever it comes across a multiple of 3, 5 or both/ why is this so?
input = fizzBuzz(100)
console.log(input)
function fizzBuzz(limit){
for (let i = 0; i <= limit; ++i)
if (i % 3 === 0 && i % 5 === 0)
console.log('fizzbuzz')
else if (i % 3 === 0)
console.log('fizz')
else if (i % 5 === 0)
console.log('buzz')
else console.log(i)
}
input = fizzBuzz(100)
console.log(input)
function fizzBuzz(limit){
for (let i = 0; i <= limit; ++i) {
if (i % 3 === 0 && i % 5 === 0)
return 'fizzbuzz'
else if (i % 3 === 0)
return 'fizz'
else if (i % 5 === 0)
return 'buzz'
else return i
}
}
I think it is because the return statement stops the function from executing anything further but I am not sure, still new and self teaching!