Simple Thought Exercise
Imagine you have a laundry basket full of freshly cleaned clothes
You have a bunch of different items of clothing to fold and put away
We will fold the clothes and sort them into separate baskets
Do we know the difference between these items of clothing?
What about these items of clothing?
Do we fold these the same way?
Depending on the item of clothing, we will fold it differently
if(item === 'pants') {
// fold pants
} else if(item === 'shorts') {
// fold shorts
} else if(item === 'skirt') {
// fold skirt
} else if(item === 'socks') {
// fold socks
} else if(item === 'shirt') {
// fold dark shirt
}
Depending on who the item of clothing belongs to, we will sort it into a different basket
if(owner === 'dad') {
// sort into dad's basket
} else if(owner === 'mom') {
// sort into mom's basket
} else if(owner === 'sister') {
// sort into sister's basket
} else if(owner === 'brother') {
// sort into brother's basket
} else if(owner === 'me') {
// sort into my basket
}
We will do the same thing for each item of clothing
shirts.forEach(shirt => fold(shirt));
The entire process might look something like this...
First, we decide how we are going to fold each item of clothing
const foldPants = pants => {
// fold pants
};
const foldShorts = shorts => {
// fold shorts
};
const foldSkirt = skirt => {
// fold skirt
};
const foldSocks = socks => {
// fold socks
};
const foldShirt = shirt => {
// fold shirt
};
Next, we loop through each item of clothing and fold it
const foldedItems = items.forEach(item => {
if(item === 'pants') {
return foldPants(item);
} else if(item === 'shorts') {
return foldShorts(item);
} else if(item === 'skirt') {
return foldSkirt(item);
} else if(item === 'socks') {
return foldSocks(item);
} else if(item === 'shirt') {
return foldShirt(item);
}
});
Finally, we loop through the folded clothing and put it in the correct basket
const sortedItems = foldedItems.forEach(item => {
if(item.owner === 'dad') {
dadBasket.add(item);
} else if(item.owner === 'mom') {
momBasket.add(item);
} else if(item.owner === 'sister') {
sisterBasket.add(item);
} else if(item.owner === 'brother') {
brotherBasket.add(item);
} else if(item.owner === 'me') {
myBasket.add(item);
}
});
🎉 We have just covered the four elements of programming! 🎉