Loops II - Activity
Make sure you attempt the activity on your own before checking out the solution.
Instructions
Create a flowchart to represent the following program, then code it out.
- Using a loop, prompt the user to enter five names.
- Push each name to a
nameList
array. - Using another loop, render/alert the five student names.
Challenge:
Don't forget to flowchart FIRST!
- Prompt the user for a number of students in the class.
- Prompt the user to input that many names.
- Render all of the names.
Ultra Challenge:
Working off of the bonus, print only the people whose names include the letter a
.
Solution
const nameList = [];
for (let i = 0; i < 5; i++){
const name = prompt('Enter a name');
nameList.push(name);
}
for (let i = 0; i < nameList.length; i++){
const name = nameList[i];
alert(name);
}
// Challenge
const challengeList = [];
const challengeQuestion = parseInt(prompt('How many people are in the class?'));
for (let i = 0; i < challengeQuestion; i++) {
const newName = prompt(`Enter person number ${i+1}'s name:`);
challengeList.push(newName);
}
for (let i = 0; i < challengeList.length; i++) {
console.log(challengeList[i]);
}
// ULTRA CHALLENGE
for (let i = 0; i < challengeList.length; i++) {
if (challengeList[i].includes('a')) {
console.log(challengeList[i]);
}
}