FizzBuzz in 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.

unittest:

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);
});

});

THIS IS MY SOLUTION BELOW:

var fizzBuzz = function(){
for(i = 1; i < 102; i++){
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);

}

}
};

HERE IS THE TEST RESULT:

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.

Could someone please kindly help me fix this!


#2

When doing FizzBuzz, you need to have the code for returning FizzBuzz before Fizz or Buzz individually. Otherwise, when you get to say 15, it's going to test if it's divisible by three, find out it is, log "Fizz" and then move on.


#3

Alright, thanks. I've just done that, but its still giving me same error message as display above.


#4

It works for me, all I did was copy and paste the pieces of your code into the correct order and formatted for readability, I haven't changed it otherwise:

var fizzBuzz = function(){
for(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);
}

}
};

fizzBuzz();


#5

Here is the test solution error I got:

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
{"failures":[{"failedSpecNumber":1,"fullName":"Fizz Buzz tests should return Fizz for number divisible by 3","failedExpectations":[{"message":"Expected undefined to be 'Fizz'."}]},{"failedSpecNumber":2,"fullName":"Fizz Buzz tests should return Buzz for number divisible by 5","failedExpectations":[{"message":"Expected undefined to be 'Buzz'."}]},{"failedSpecNumber":3,"fullName":"Fizz Buzz tests should return FizzBuzz for 15","failedExpectations":[{"message":"Expected undefined to be 'FizzBuzz'."}]},{"failedSpecNumber":4,"fullName":"Fizz Buzz tests should return FizzBuzz for 45","failedExpectations":[{"message":"Expected undefined to be 'FizzBuzz'."}]},{"failedSpecNumber":5,"fullName":"Fizz Buzz tests should return FizzBuzz for 90","failedExpectations":[{"message":"Expected undefined to be 'FizzBuzz'."}]},{"failedSpecNumber":6,"fullName":"Fizz Buzz tests should return Fizz for 63","failedExpectations":[{"message":"Expected undefined to be 'Fizz'."}]},{"failedSpecNumber":7,"fullName":"Fizz Buzz tests should return 7 since its indivisible by 3 and 5","failedExpectations":[{"message":"Expected undefined to be 7."}]},{"failedSpecNumber":8,"fullName":"Fizz Buzz tests should return 101 since its indivisible by 3 and 5","failedExpectations":[{"message":"Expected undefined to be 101."}]}],"success":[],"pending":[],"started":true,"finished":true,"specs":{"count":8,"pendingCount":0,"time":0.041},"passed":false}


#6

Thanks skeptabomb, i appreciate your effort.


#7

Hello, has anyone managed to run the above code successfully? Kindly assist am also stuck?
I get this unittest error:
Total Specs: 8 Total Failures: 5
1 . Fizz Buzz tests should return Fizz for number divisible by 3
Expected 'FizzBuzz' to be 'Fizz'.
2 . Fizz Buzz tests should return Buzz for number divisible by 5
Expected 'FizzBuzz' to be 'Buzz'.
3 . Fizz Buzz tests should return Fizz for 63
Expected 'FizzBuzz' to be 'Fizz'.
4 . Fizz Buzz tests should return 7 since its indivisible by 3 and 5
Expected 'FizzBuzz' to be 7.
5 . Fizz Buzz tests should return 101 since its indivisible by 3 and 5
Expected 'FizzBuzz' to be 101.

HERE IS MY CODE:
function fizzBuzz(){
for(var i=1;i<=100;i++){
if(15%5 === 0 && 15%3 === 0){
return('FizzBuzz');
} else if(63%3 === 0){
return('Fizz');
} else if(20%5 === 0){
return('Buzz');
}else if (101||3&&5){
return(101);
}
}
}


#8

hello i need help with the question too


#9