FizzBuzz using Javascript


#1

Hi, Could someone please help me with this. I'm actually learning programming and I'm faced with this challenge.

Create a function fizzBuzz to return 'Fizz', 'Buzz', 'FizzBuzz', or the argument it receives, all depending on the argument of the function, a number that is divisible by, 3, 5, or both 3 and 5, respectively.
When the number is not divisible by 3 or 5, the number itself should be returned.

it("should return Fizz for number divisible by 3", function() {
expect(fizzBuzz(3)).toBe('Fizz');
});

it("should return Buzz for number divisible by 5", function() {
expect(fizzBuzz(5)).toBe('Buzz');
});

it("should return FizzBuzz for 15", function() {
expect(fizzBuzz(15)).toBe('FizzBuzz');
});

it("should return FizzBuzz for 45", function() {
expect(fizzBuzz(45)).toBe('FizzBuzz');
});

it("should return FizzBuzz for 90", function() {
expect(fizzBuzz(90)).toBe('FizzBuzz');
});

it("should return Fizz for 63", function() {
expect(fizzBuzz(63)).toBe('Fizz');
});

it("should return 7 since its indivisible by 3 and 5", function() {
expect(fizzBuzz(7)).toBe(7);
});

it("should return 101 since its indivisible by 3 and 5", function() {
expect(fizzBuzz(101)).toBe(101);
});

});

Here is my solution:
var fizzBuzz = function(){
var text = "";
var i;

for(i = 1; i < 102; i++){
 text += i + "<br>";
 if (i % 3 === 0){
    console.log("Fizz");
}
else if(i % 5 === 0){
    console.log("Buzz");
}
else if(i % 3 === 0 && i % 5 === 0){
    console.log("FizzBuzz");
}
else{
    console.log(i);
    
}

}
};

Error Message:
var fizzBuzz = function(){
var text = "";
var i;

for(i = 1; i < 102; i++){
 text += i + "<br>";
 if (i % 3 === 0){
    console.log("Fizz");
}
else if(i % 5 === 0){
    console.log("Buzz");
}
else if(i % 3 === 0 && i % 5 === 0){
    console.log("FizzBuzz");
}
else{
    console.log(i);
    
}

}
};

Total Specs: 8 Total Failures: 81
.
Fizz Buzz tests should return Fizz for number divisible by 3Expected undefined to be 'Fizz'.2
.
Fizz Buzz tests should return Buzz for number divisible by 5Expected undefined to be 'Buzz'.3
.
Fizz Buzz tests should return FizzBuzz for 15Expected undefined to be 'FizzBuzz'.4
.
Fizz Buzz tests should return FizzBuzz for 45Expected undefined to be 'FizzBuzz'.5
.
Fizz Buzz tests should return FizzBuzz for 90Expected undefined to be 'FizzBuzz'.6
.
Fizz Buzz tests should return Fizz for 63Expected undefined to be 'Fizz'.7
.
Fizz Buzz tests should return 7 since its indivisible by 3 and 5Expected undefined to be 7.8
.
Fizz Buzz tests should return 101 since its indivisible by 3 and 5Expected undefined to be 101.


#2

This needs to be checked first.

What is this line doing?? Have you defined text anywhere else? I would just remove it


#3

Yes that's true, that line of code is actually not necessary. Have taken it off and my answer still remains the same. Is like am missing something out.


#4

@ujerry2013gmail.com

I got it work by removing the wrapping function.

for(var i = 1; i < 102; i++){
if (i % 3 === 0 && i % 5 === 0){
console.log("FizzBuzz");
}else if(i % 5 === 0){
console.log("Buzz");
}else if(i % 3 === 0){
console.log("Fizz");
}else{
console.log(i);
}
}

#5

I try running this code , here is the Test Solution Error am getting.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
101

/home/ubuntu/Applications/andelabs-server/tmp/andelabs_566594a22e01d71500ffccf9_565c28fd9c306a14009680f2.js:16
module.exports = fizzBuzz;
^
ReferenceError: fizzBuzz is not defined
at Object. (/home/ubuntu/Applications/andelabs-server/tmp/andelabs_566594a22e01d71500ffccf9_565c28fd9c306a14009680f2.js:16:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.


#6

Thanks benjnev for your concern, I appreciate.


#7

for (let n = 1; n <= 100; n++) {
let output = “”;
if (n % 3 == 0) output += “Fizz”;
if (n % 5 == 0) output += “Buzz”;
console.log(output || n);
}
The console will print either the value of output or n. If the value of output is a false value(Empty String) it will print the value of n. If the value of the output is a true value(String has a value) then it will print the value of output.


#8

Hi Verunar,

If you have a question or an error please create a new topic with full information.

Thanks


#9