Contenido del Curso
Introduction to TypeScript
Introduction to TypeScript
Challenge (FizzBuzz)
Task
The classic problem is called FizzBuzz. The task is very simple: You are given an array of random numbers, and the code for generating the numbers is provided above; please do not modify that code. Your task is to replace the array elements according to the following conditions:
- If a number is divisible by 3, replace it with '
Fizz
'; - If a number is divisible by 5, replace it with '
Buzz
'; - If a number is divisible by both 3 and 5, replace it with '
FizzBuzz
'.
As a result, you should return an array of numbers and strings. You can check the hints and solutions if you have any difficulties with solving this problem. This way, you will better absorb the information and improve your skills. May the force be with you!
let randomNumbers: (number | string)[] = []; // do not change the code below for (let i = 0; i < 15; i++) { let randomNumber: number = Math.floor(Math.random() * 100); // Generating a random number from 0 to 99 randomNumbers.push(randomNumber); } //do not change the code above for (let i = 0; i < ___; i++) { // Assert that randomNumbers[i] is a number before using the modulus operator let num = randomNumbers[i] as number; if (___) { randomNumbers[i] = '___' } else if (___) { ___ = 'Fizz' } else if (___) { ___ } } console.log(randomNumbers);
¡Gracias por tus comentarios!