scrimba
Helen Chong's JavaScriptmas 2023 Solutions
Helen Chong's JavaScriptmas 2023 Day 16 solution
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Helen Chong's JavaScriptmas 2023 Day 16 solution
AboutCommentsNotes
Helen Chong's JavaScriptmas 2023 Day 16 solution
Expand for more info
index.js
run
preview
console
const niceList = document.getElementById("nice-list")
const naughtyList = document.getElementById("naughty-list")
const btn = document.getElementById("btn")
const li = document.createElement("li")

btn.addEventListener("click", sort)

const sorteesArr = [
{
name: "David",
hasBeenGood: false
},
{
name: "Del",
hasBeenGood: true
},
{
name: "Valerie",
hasBeenGood: false
},
{
name: "Astrid",
hasBeenGood: true
}
]

/** Challenge:
- Write the JavaScript to sort the people in sorteesArr into the naughty and nice lists, according to whether they have been good or not. Then display the names in the relevant place in the DOM.
**/

/** Stretch goals:
- Add the option to add new names to the sorteesArr.
- Make it possible to switch people to the other list.
**/

function sort() {
const niceListArr = sorteesArr.filter(sortee => sortee.hasBeenGood)
const naughtyListArr = sorteesArr.filter(sortee => !sortee.hasBeenGood)

niceListArr.forEach((nice) => {
console.log(`${nice.name} has been nice`)
niceList.innerHTML += `<li>${nice.name}</li>`
})

naughtyListArr.forEach((naughty) => {
console.log(`${naughty.name} has been naughty`)
naughtyList.innerHTML += `<li>${naughty.name}</li>`
})
}
Console
"Del has been nice"
,
"Astrid has been nice"
,
"David has been naughty"
,
"Valerie has been naughty"
,
/index.html
LIVE