Files
freeCodeCamp/curriculum/challenges/italian/22-rosetta-code/rosetta-code-challenges/fizzbuzz.md
2024-01-24 19:52:36 +01:00

1.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5e9ddb06ec35240f39657419 FizzBuzz 1 385370 fizzbuzz

--description--

Write a program that generates an array of integers from 1 to 100 (inclusive). But:

  • for multiples of 3, add "Fizz" to the array instead of the number
  • per multipli di 5, aggiungi "Buzz" all'array invece del numero
  • per multipli di 3 e 5, aggiungi "FizzBuzz" all'array invece del numero

--instructions--

Il tuo programma dovrebbe restituire un array contenete i risultati seguendo le regole scritte sopra.

--hints--

fizzBuzz dovrebbe essere una funzione.

assert(typeof fizzBuzz == 'function');

fizzBuzz() dovrebbe restituire un array.

assert(Array.isArray(fizzBuzz()) == true);

I numeri divisibili per solo 3 dovrebbero restituire "Fizz".

assert.equal(fizzBuzz()[2], 'Fizz');

I numeri divisibili per solo 5 dovrebbero restituire "Buzz".

assert.equal(fizzBuzz()[99], 'Buzz');

Numeri divisibili per 3 e 5 dovrebbero restituire "FizzBuzz".

assert.equal(fizzBuzz()[89], 'FizzBuzz');

I numeri non divisibili per 3 o 5 dovrebbero restituire il numero stesso.

assert.equal(fizzBuzz()[12], 13);

--seed--

--seed-contents--

function fizzBuzz() {

}

--solutions--

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