Question: (javascript) Have the function StringChallenge( num ) take the num parameter being passed and return all the numbers from 1 to num separated by spaces,
(javascript) Have the function StringChallenge(num) take the num parameter being passed and return all the numbers from 1 to num separated by spaces, but replace every number that is divisible by 3 with the word "Fizz", replace every number that is divisible by 5 with the word "Buzz", and every number that is divisible by both 3 and 5 with the word "FizzBuzz".
For example: if num is 16, then your program should return the string "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16". The input will be within the range 1 - 50.
I need assistance with making this code run, please help me fix it. I get console issue saying "nums is not defined at Object.
function StringChallenge(num) {
// code goes here
//if number is not positive, return empty string
if (num <= 0) return "";
var nums = "1"; (issue starts here)
//run loop from 2 to num
//if i divisible by 15 (both 3 & 5), add Fizzbuzz
//otherwise add Fizz, Buzz, or i
for (let i=2; i <= num; i++)
if (i % 15 == 0) nums+= "FizzBuzz";
else if (i % 3 == 0) nums+= "Fizz";
else if (i % 5 == 0) nums+= "Buzz";
else nums += " " + i;
}
return nums;
// keep this function call here
console.log(StringChallenge(readline(nums)));
This is the original starter code without my solution.
function StringChallenge(num) {
//code goes here
return nums;
// keep this function call here
console.log(StringChallenge(readline( )));
Examples
Input: 3 Output: 1 2 Fizz
Input: 8 Output: 1 2 Fizz 4 Buzz Fizz 7 8
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
