const favSingers = [
"Yuna",
"Britney Spears",
"Backsteet Boys",
"Spice Girls",
"Meatloaf"
];
- programmatically print out the numbers
- generate numbers from 1 to n (the number of elements inside the array)
- print out
"My #n favourite singer is <name of singer>"
for (let i = 0; i < favSingers.length; i++) {
console.log("My #" + (i + 1) + " favourite singer is " + favSingers[i]);
}
- Keep track of elements that have been printed
- Once an element has been printed, remove it from the main array
- Pick a random one from the remaining elements in the array
- Repeat until no elements are left
let arrCopy = [...favSingers];
// Repeat this program as long as there are still elements in the array
while (arrCopy.length > 0) {
let randomDigit = Math.floor(Math.random() * arrCopy.length);
// USE BACK TICKS WOOOHOOOO
console.log(`My #${randomDigit + 1} favourite singer is ${arrCopy[randomDigit]}.`);
arrCopy.splice(randomDigit, 1);
}
- Similar process as above
- Programmatically generate a list of numbers 1 - N, N = length of the array
favSingers.splice(1, 1, "Bruno Mars");