Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
<body>
<section id="images">
<div>
<img src="images/mashed-potatoes.jpg" id="mashedPotatoesImg" hidden />
<img src="images/mashed-potatoes.jpg" id="mashedPotatoesImg"/>
</div>
<div>
<img src="images/steak.jpg" id="steakImg" hidden />
<img src="images/steak.jpg" id="steakImg"/>
</div>
<div>
<img src="images/brussels-sprouts.jpg" id="brusselsSproutsImg" hidden />
<img src="images/brussels-sprouts.jpg" id="brusselsSproutsImg"/>
</div>
<div>
<img src="images/broccoli.jpg" id="broccoliImg" hidden />
<img src="images/broccoli.jpg" id="broccoliImg"/>
</div>
</section>
<hr />
Expand Down
196 changes: 174 additions & 22 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,189 @@
// 🚨🚨🚨 Comment out the below code before you start working on the code

// Out of sync
getInstruction("mashedPotatoes", 0, (step1) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step1}</li>`;
}, (error) => console.log(error));
getInstruction("mashedPotatoes", 1, (step2) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step2}</li>`;
}, (error) => console.log(error));
getInstruction("mashedPotatoes", 2, (step3) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step3}</li>`;
}, (error) => console.log(error));
getInstruction("mashedPotatoes", 3, (step4) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step4}</li>`;
}, (error) => console.log(error));
getInstruction("mashedPotatoes", 4, (step5) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step5}</li>`;
document.querySelector("#mashedPotatoesImg").removeAttribute("hidden");
}, (error) => console.log(error));
// getInstruction("mashedPotatoes", 0, (step1) => {
// document.querySelector("#mashedPotatoes").innerHTML += `<li>${step1}</li>`;
// }, (error) => console.log(error));

// getInstruction("mashedPotatoes", 1, (step2) => {
// document.querySelector("#mashedPotatoes").innerHTML += `<li>${step2}</li>`;
// }, (error) => console.log(error));

// getInstruction("mashedPotatoes", 2, (step3) => {
// document.querySelector("#mashedPotatoes").innerHTML += `<li>${step3}</li>`;
// }, (error) => console.log(error));

// getInstruction("mashedPotatoes", 3, (step4) => {
// document.querySelector("#mashedPotatoes").innerHTML += `<li>${step4}</li>`;
// }, (error) => console.log(error));

// getInstruction("mashedPotatoes", 4, (step5) => {
// document.querySelector("#mashedPotatoes").innerHTML += `<li>${step5}</li>`;
// document.querySelector("#mashedPotatoesImg").removeAttribute("hidden");
// }, (error) => console.log(error));



// Iteration 1 - using callbacks
// ...
//
// getInstruction(food, step, callback, errorCallback)
//

getInstruction('mashedPotatoes', 0, (step0) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step0}</li>`;

getInstruction('mashedPotatoes', 1, (step1) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step1}</li>`;

getInstruction('mashedPotatoes', 2, (step2) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step2}</li>`;

getInstruction('mashedPotatoes', 3, (step3) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step3}</li>`;

getInstruction('mashedPotatoes', 4, (step4) => {
document.querySelector("#mashedPotatoes").innerHTML += `<li>${step4}</li>`;

}, () => {
console.log('error preparing mashedPotatoes step 4....');

})

}, () => {
console.log('error preparing mashedPotatoes step 3....');

})
}, () => {
console.log('error preparing mashedPotatoes step 2....');

})
}, () => {
console.log('error preparing mashedPotatoes step 1....');

})
}, () => {
console.log('error preparing mashedPotatoes step 0....');
})




// Iteration 2 - using promises
// ...
// function obtainInstruction(food, step)
//
obtainInstruction('steak', 0)

.then((step0) => {
document.querySelector("#steak").innerHTML += `<li>${step0}</li>`;
return obtainInstruction('steak', 1);
})

.then((step1) => {
document.querySelector("#steak").innerHTML += `<li>${step1}</li>`;
return obtainInstruction('steak', 2);
})

.then((step2) => {
document.querySelector("#steak").innerHTML += `<li>${step2}</li>`;
return obtainInstruction('steak', 3);
})

.then((step3) => {
document.querySelector("#steak").innerHTML += `<li>${step3}</li>`;
return obtainInstruction('steak', 4);
})

.then((step4) => {
document.querySelector("#steak").innerHTML += `<li>${step4}</li>`;
return obtainInstruction('steak', 5);
})

.then((step5) => {
document.querySelector("#steak").innerHTML += `<li>${step5}</li>`;
return obtainInstruction('steak', 6);
})

.then((step6) => {
document.querySelector("#steak").innerHTML += `<li>${step6}</li>`;
return obtainInstruction('steak', 7);
})

.then((step7) => {
document.querySelector("#steak").innerHTML += `<li>${step7}</li>`;
return obtainInstruction('steak', 8);
})

.catch(function(error) {});






// Iteration 3 using async/await
// ...
// async function makeBroccoli()
//
async function makeBroccoli() {
try{

const step0 = await obtainInstruction('broccoli', 0);
document.querySelector("#broccoli").innerHTML += `<li>${step0}</li>`;

const step1 = await obtainInstruction('broccoli', 1);
document.querySelector("#broccoli").innerHTML += `<li>${step1}</li>`;

const step2 = await obtainInstruction('broccoli', 2);
document.querySelector("#broccoli").innerHTML += `<li>${step2}</li>`;

const step3 = await obtainInstruction('broccoli', 3);
document.querySelector("#broccoli").innerHTML += `<li>${step3}</li>`;

const step4 = await obtainInstruction('broccoli', 4);
document.querySelector("#broccoli").innerHTML += `<li>${step4}</li>`;

const step5 = await obtainInstruction('broccoli', 5);
document.querySelector("#broccoli").innerHTML += `<li>${step5}</li>`;

const step6 = await obtainInstruction('broccoli', 6);
document.querySelector("#broccoli").innerHTML += `<li>${step6}</li>`;

document.querySelector("#broccoli").innerHTML += `<li>Broccoli is ready!</li>`;
} catch(error) {
console.log(error);
}
}

makeBroccoli();


// Bonus 2 - Promise all
// ...
// function makeBrusselsSprouts()
//

function makeBrusselsSprouts() {
const steps = [
obtainInstruction('brusselsSprouts', 0),
obtainInstruction('brusselsSprouts', 1),
obtainInstruction('brusselsSprouts', 2),
obtainInstruction('brusselsSprouts', 3),
obtainInstruction('brusselsSprouts', 4),
obtainInstruction('brusselsSprouts', 5),
obtainInstruction('brusselsSprouts', 6),
obtainInstruction('brusselsSprouts', 7)
];

Promise.all(steps)
.then((allSteps) => {
// allSteps es un array con los textos de cada instrucción
allSteps.forEach((step) => {
document.querySelector("#brusselsSprouts").innerHTML += `<li>${step}</li>`;
});
// mensaje final
document.querySelector("#brusselsSprouts").innerHTML += `<li>Brussels sprouts are ready!</li>`;
})
.catch((error) => console.log(error));
}

makeBrusselsSprouts();
1 change: 1 addition & 0 deletions javascript/obtainInstruction.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function obtainInstruction(food, step) {
// Get the instruction string
let instruction;


if (food === "mashedPotatoes") {
instruction = mashedPotatoes[step];
}
Expand Down