From 67fbe40cb58608fe0f9e5316c2c6c81ca5d0aa24 Mon Sep 17 00:00:00 2001 From: paujorbaa Date: Mon, 29 Sep 2025 15:24:58 +0200 Subject: [PATCH] done --- javascript/index.js | 141 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 135 insertions(+), 6 deletions(-) diff --git a/javascript/index.js b/javascript/index.js index 9f30552db..108789177 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -4,6 +4,7 @@ // 🚨🚨🚨 Comment out the below code before you start working on the code // Out of sync +/* getInstruction("mashedPotatoes", 0, (step1) => { document.querySelector("#mashedPotatoes").innerHTML += `
  • ${step1}
  • `; }, (error) => console.log(error)); @@ -24,17 +25,145 @@ document.querySelector("#mashedPotatoes").innerHTML += `
  • ${step5}
  • `; document.querySelector("#mashedPotatoesImg").removeAttribute("hidden"); }, (error) => console.log(error)); - - +*/ // Iteration 1 - using callbacks -// ... +getInstruction( + "mashedPotatoes", + 0, + (step1) => { + document.querySelector("#mashedPotatoes").innerHTML += `
  • ${step1}
  • `; + getInstruction( + "mashedPotatoes", + 1, + (step2) => { + document.querySelector( + "#mashedPotatoes" + ).innerHTML += `
  • ${step2}
  • `; + getInstruction( + "mashedPotatoes", + 2, + (step3) => { + document.querySelector( + "#mashedPotatoes" + ).innerHTML += `
  • ${step3}
  • `; + getInstruction( + "mashedPotatoes", + 3, + (step4) => { + document.querySelector( + "#mashedPotatoes" + ).innerHTML += `
  • ${step4}
  • `; + getInstruction( + "mashedPotatoes", + 4, + (step5) => { + document.querySelector( + "#mashedPotatoes" + ).innerHTML += `
  • ${step5}
  • `; + document + .querySelector("#mashedPotatoesImg") + .removeAttribute("hidden"); + + document.querySelector( + "#mashedPotatoes" + ).innerHTML += `
  • Mashed potatoes are ready!
  • `; + }, + (error) => console.log(error) + ); + }, + (error) => console.log(error) + ); + }, + (error) => console.log(error) + ); + }, + (error) => console.log(error) + ); + }, + (error) => console.log(error) +); // Iteration 2 - using promises -// ... + +obtainInstruction("steak", 0) + .then((step0) => { + document.querySelector("#steak").innerHTML += `
  • ${step0}
  • `; + return obtainInstruction("steak", 1); + }) + .then((step1) => { + document.querySelector("#steak").innerHTML += `
  • ${step1}
  • `; + return obtainInstruction("steak", 2); + }) + .then((step2) => { + document.querySelector("#steak").innerHTML += `
  • ${step2}
  • `; + return obtainInstruction("steak", 3); + }) + .then((step3) => { + document.querySelector("#steak").innerHTML += `
  • ${step3}
  • `; + return obtainInstruction("steak", 4); + }) + .then((step4) => { + document.querySelector("#steak").innerHTML += `
  • ${step4}
  • `; + return obtainInstruction("steak", 5); + }) + .then((step5) => { + document.querySelector("#steak").innerHTML += `
  • ${step5}
  • `; + return obtainInstruction("steak", 6); + }) + .then((step6) => { + document.querySelector("#steak").innerHTML += `
  • ${step6}
  • `; + return obtainInstruction("steak", 7); + }) + .then((step7) => { + document.querySelector("#steak").innerHTML += `
  • ${step7}
  • `; + document.querySelector( + "#steak" + ).innerHTML += `
  • Steak is ready!
  • `; + document.querySelector("#steakImg").removeAttribute("hidden"); + }) + + .catch((error) => console.log(error)); // Iteration 3 using async/await -// ... +async function makeBroccoli() { + try { + for (let i = 0; i <= 6; i++) { + const step = await obtainInstruction("broccoli", i); + document.querySelector("#broccoli").innerHTML += `
  • ${step}
  • `; + } + document.querySelector( + "#broccoli" + ).innerHTML += `
  • Broccoli is ready!
  • `; + document.querySelector("#broccoliImg").removeAttribute("hidden"); + } catch (error) { + console.log(error); + } +} +makeBroccoli(); // Bonus 2 - Promise all -// ... \ No newline at end of file +function makeBrusselsSprouts() { + const steps = []; + + for (let i = 0; i <= 7; i++) { + steps.push(obtainInstruction("brusselsSprouts", i)); + } + + Promise.all(steps) + .then((instructions) => { + instructions.forEach((step) => { + document.querySelector( + "#brusselsSprouts" + ).innerHTML += `
  • ${step}
  • `; + }); + + document.querySelector( + "#brusselsSprouts" + ).innerHTML += `
  • Brussels sprouts are ready!
  • `; + document.querySelector("#brusselsSproutsImg").removeAttribute("hidden"); + }) + .catch((error) => console.log(error)); +} + +makeBrusselsSprouts();