Skip to content

Instantly share code, notes, and snippets.

@colevandersWands
Last active April 21, 2018 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colevandersWands/a25901206cf04f266afde7da8b7aa152 to your computer and use it in GitHub Desktop.
Save colevandersWands/a25901206cf04f266afde7da8b7aa152 to your computer and use it in GitHub Desktop.
implementing different loops for the same challenge
Loops
behavioral specifications:
(think of an ideal use case for a loop)
- eating, you do it everyday
weekly meal eater
code specs
args: 1
array of 7 arrays
nested array contains 3 meals, String
returns: array
array of Strings
behavior:
consumes one food item per meal
returns all poo at the end of the week. assume a healthy daily bowl movement schedule
https://www.w3schools.com/js/js_loop_for.asp
(run these in pythontutor)
function eater(all_meals) {
let returner = [];
let day_cs = 0;
let meal = 0;
do { // 7 days
do { // 3 meals
all_meals[day_cs][meal] = "clean plate"
meal++;
} while (meal < 3)
meal = 0;
returner.push("pooo");
day_cs++;
} while (day_cs < 7)
console.log(all_meals);
return returner;
}
let meals = [
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"]]
console.log(eater(meals))
function eater(all_meals) {
let returner = [];
for (let day = 0; day < 7; day++) {
for (let meal = 0; meal < 3; meal++) {
all_meals[day][meal] = "clean plate"
}
returner.push("pooo");
}
console.log(all_meals);
return returner;
}
let meals = [
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"]]
console.log(eater(meals))
function eater(all_meals) {
let returner = [];
for (var day = 0; day < 7; day++) {
for (var meal = 0; meal < 3; meal++) {
all_meals[day][meal] = "clean plate"
}
returner.push("pooo");
}
console.log(all_meals);
return returner;
}
let meals = [
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"]]
console.log(eater(meals))
function eater(all_meals) {
let returner = [];
let day_cs = 0;
let meal = 0;
while (day_cs < 7) { // 7 days
while (meal < 3) { // 3 meals
all_meals[day_cs][meal] = "clean plate"
meal++;
}
meal = 0;
returner.push("pooo");
day_cs++;
}
console.log(all_meals);
return returner;
}
let meals = [
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"],
[
"cheese",
"tomatoes",
"pickles"]]
console.log(eater(meals))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment