I'm a total newbie to computer science. I beg for patience.
I've been following this tutorial and I'm on the first "active learning example."
const list = document.querySelector('.output ul');
list.innerHTML = '';
let greetings = ['Happy Birthday!',
'Merry Christmas my love',
'A happy Christmas to all the family',
'You\'re all I want for Christmas',
'Get well soon'];
for (let i = 0; i < greetings.length; i++) {
let input = greetings[i];
// Your conditional test needs to go inside the parentheses
// in the line below, replacing what's currently there
if (greetings[i]) {
let listItem = document.createElement('li');
listItem.textContent = input;
list.appendChild(listItem);
}
}
The goal is to modify the above code so the live output will display only the array values that include 'Christmas'. This is my solution:
for (let i = 0; i < greetings.length; i++) {
let input = greetings[i];
if (greetings[i].indexOf('Christmas') !== -1) {
let listItem = document.createElement('li');
listItem.textContent = input;
list.appendChild(listItem);
}
}
This is the tutorial's solution (the same except for the introduction of a new variable - "result"):
for (let i = 0; i < greetings.length; i++) {
let input = greetings[i];
if (greetings[i].indexOf('Christmas') !== -1) {
let result = input;
let listItem = document.createElement('li');
listItem.textContent = result;
list.appendChild(listItem);
}
Is it necessary to introduce "result", when it's just going to be changed to "input" anyways?
Aucun commentaire:
Enregistrer un commentaire