JavaScript Excercise: Lucian's Luscious Lasagna

Found this simple JS exercise. Testing how many will be interested in solving and discussing it on OF

Instructions

Lucian’s girlfriend is on her way home and he hasn’t cooked their anniversary dinner!

In this exercise, you’re going to write some code to help Lucian cook an exquisite lasagna from his favorite cook book.

You have four tasks, all related to the time spent cooking the lasagna.

1. Define the expected oven time in minutes

Define the Lasagna.ExpectedMinutesInOven() method that does not take any parameters and returns how many minutes the lasagna should be in the oven. According to the cooking book, the expected oven time in minutes is 40:

var lasagna = new Lasagna();
lasagna.ExpectedMinutesInOven();
// => 40

2. Calculate the remaining oven time in minutes

Define the Lasagna.RemainingMinutesInOven() method that takes the actual minutes the lasagna has been in the oven as a parameter and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.

var lasagna = new Lasagna();
lasagna.RemainingMinutesInOven(30);
// => 10

3. Calculate the preparation time in minutes

Define the Lasagna.PreparationTimeInMinutes() method that takes the number of layers you added to the lasagna as a parameter and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.

var lasagna = new Lasagna();
lasagna.PreparationTimeInMinutes(2);
// => 4

4. Calculate the elapsed time in minutes

Define the Lasagna.ElapsedTimeInMinutes() method that takes two parameters: the first parameter is the number of layers you added to the lasagna, and the second parameter is the number of minutes the lasagna has been in the oven. The function should return how many minutes you’ve worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.

var lasagna = new Lasagna();
lasagna.ElapsedTimeInMinutes(3, 20);
// => 26

Any takers?

3 Likes

Yes, I’ll give it a go. Stay tuned!

1 Like

Don’t know anything about JS but will give this a try.

1 Like

The more the merrier. :smiley:

You can present your solution when you have it and we’ll analyse together with @danielamissah

2 Likes

@siaw ano dey see top ooo. Can’t find my way around it. Any ideas

OK no worries. It’s late here let me write my solution then we can talk. :wink:

Sorry @danielamissah, I was caught up in TV series and senseless threads on OF. :rofl:
Just wrote my solution. Would you like to tell me what you’re having problems with so we do it together? Or you want me to paste my solution? :crazy_face:

@siaw , just figuring it out is the whole bit for me. I kinda have something running (in my mind) but when I put it in my text editor, then beans

OK no worries. Here’s what I have. We don’t need it to be object-oriented like in the instructions. Please have a look at this if you understand everything:

/**
 * The number of minutes it takes to prepare a single layer.
 */
const PREPARATION_MINUTES_PER_LAYER = 2;
export const EXPECTED_MINUTES_IN_OVEN = 40;

/**
 * Determines the number of minutes the lasagna still needs to remain in the
 * oven to be properly prepared.
 *
 * @param {number} actualMinutesInOven
 * @returns {number} the number of minutes remaining
 */
export function remainingMinutesInOven(actualMinutesInOven) {
  return EXPECTED_MINUTES_IN_OVEN - actualMinutesInOven
}

/**
 * Given a number of layers, determines the total preparation time.
 *
 * @param {number} numberOfLayers
 * @returns {number} the total preparation time
 */
export function preparationTimeInMinutes(numberOfLayers) {
  return PREPARATION_MINUTES_PER_LAYER * numberOfLayers
}

/**
 * Calculates the total working time. That is, the time to prepare all the layers
 * of lasagna, and the time already spent in the oven.
 *
 * @param {number} numberOfLayers
 * @param {number} actualMinutesInOven
 * @returns {number} the total working time
 */
export function totalTimeInMinutes(numberOfLayers, actualMinutesInOven) {
  return preparationTimeInMinutes(numberOfLayers) + actualMinutesInOven
}