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!