2020-05-11 05:40:44 -07:00
|
|
|
---
|
|
|
|
id: 5e9ddb06ec35240f39657419
|
2020-11-27 19:02:05 +01:00
|
|
|
title: FizzBuzz
|
2020-05-11 05:40:44 -07:00
|
|
|
challengeType: 5
|
|
|
|
forumTopicId: 385370
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: fizzbuzz
|
2020-05-11 05:40:44 -07:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
2020-05-11 05:40:44 -07:00
|
|
|
|
|
|
|
Write a program that generates an array of integers from 1 to 100 (inclusive). But:
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2020-05-11 05:40:44 -07:00
|
|
|
<ul>
|
|
|
|
<li>for multiples of 3, add <code>"Fizz"</code> to the array instead of the number</li>
|
|
|
|
<li>for multiples of 5, add <code>"Buzz"</code> to the array instead of the number</li>
|
|
|
|
<li>for multiples of 3 and 5, add <code>"FizzBuzz"</code> to the array instead of the number</li>
|
|
|
|
</ul>
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --instructions--
|
2020-05-11 05:40:44 -07:00
|
|
|
|
|
|
|
Your program should return an array containing the results based on the rules above.
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --hints--
|
|
|
|
|
|
|
|
`fizzBuzz` should be a function.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(typeof fizzBuzz == 'function');
|
2020-05-11 05:40:44 -07:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`fizzBuzz()` should return an Array.
|
2020-05-11 05:40:44 -07:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(Array.isArray(fizzBuzz()) == true);
|
|
|
|
```
|
2020-05-11 05:40:44 -07:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
Numbers divisible by only 3 should return `"Fizz"`.
|
2020-05-11 05:40:44 -07:00
|
|
|
|
|
|
|
```js
|
2020-11-27 19:02:05 +01:00
|
|
|
assert.equal(fizzBuzz()[2], 'Fizz');
|
|
|
|
```
|
2020-09-15 09:57:40 -07:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
Numbers divisible by only 5 should return `"Buzz"`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(fizzBuzz()[99], 'Buzz');
|
|
|
|
```
|
|
|
|
|
|
|
|
Numbers divisible by both 3 and 5 should return `"FizzBuzz"`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(fizzBuzz()[89], 'FizzBuzz');
|
|
|
|
```
|
|
|
|
|
|
|
|
Numbers not divisible by either 3 or 5 should return the number itself.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(fizzBuzz()[12], 13);
|
2020-05-11 05:40:44 -07:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --seed--
|
2020-05-11 05:40:44 -07:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --seed-contents--
|
2020-05-11 05:40:44 -07:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
function fizzBuzz() {
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
2020-05-11 05:40:44 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
function fizzBuzz() {
|
|
|
|
let res=[];
|
|
|
|
for (let i =1; i < 101; i++) {
|
|
|
|
if (i % 3 === 0 && i % 5 === 0) {
|
|
|
|
res.push("FizzBuzz");
|
|
|
|
}
|
|
|
|
else if (i % 3 === 0) {
|
|
|
|
res.push("Fizz");
|
|
|
|
}
|
|
|
|
else if (i % 5 === 0) {
|
|
|
|
res.push("Buzz");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
```
|