printInstructions function

Write a function called printInstructions.

It should take in an array of steps (strings) and log each one, with the string "Step 1 - ", "Step 2 - " in front.

printInstructions([
“Remove the plastic wrapper”,
“Microwave 45 seconds”,
“Let cool”,
“Enjoy!”,
]);

Hint : At each step, the counter variable is one less than the step number.

What have you tried so far? Accessing element of an array inside of a loop and using console log will be helpful here.

function printInstructions(string) {
     console.log(string);

let steps = ([
     "Remove the plastic wrapper",
     "Microwave 45 seconds",
     "Let cool",
     "Enjoy!",
])
     for (let i = 0; i < string[i]; i++) {
          console.log("Step 1 - " + steps);
     }
}

I tried to reply sooner but apparently I can only reply so many times in a day.

A couple of things I see here.

  1. You define steps within the function. Why?
  2. console.log("Step 1 - " + steps) would log
    Step 1 - ["Remove the plastic wrapper", "Microwave 45 seconds", "Let cool", "Enjoy!"] each time the for statement iterates.

It says to log each one, with the string "Step 1 - ", "Step 2 - " in front. I don’t understand how to code that. I understand writing functions, and I understand how to write for loops, but putting a loop in a function is what is really confusing.

Okay, let me give you a couple of tips :slight_smile:
printInstructions([“Remove the plastic wrapper”, “Microwave 45 seconds”, “Let cool”, “Enjoy!”, ]);
This is the function you provided, which is calling an array. You can ignore the stuff in the array for now - defining a variable steps in your function with these strings doesn’t help you.

In your for loop, your condition i < string[i] tells the loop to run when the value of i is less than the value of string[i]. Let’s say i=0, which it does on the first iteration of your loop. You’re asking the function to see if 0 < string[0]. string[0] is "Remove the plastic wrapper, which cannot be compared to a number.

+ serves as a concatenation operator. "Step 1 -" + tells the function to produce a string that starts with “Step 1-” on every loop. How can you modify that code to change the number?

Do you want to implement the function of the following code?

function printInstructions(steps) {
  const prefix = index => `Step ${index} - `;

  for (let i = 0, len = steps.length; i < len; i++) {
    const t = steps[i];
    console.log(prefix(i + 1) + t);
  }
}

I guess it should be.

printInstructions([“Remove the plastic wrapper”, “Microwave 45 seconds”, “Let cool”, “Enjoy!”, ]);
^ This was part of the assignment description. I don’t understand how to log the steps if you don’t define them as a variable or something.

As far as how to modify the cod to change the number, I am not sure how to write that code.

This is all still new to me.

If I have a function:
testFunction(var) {return var+3}
Then testFunction(5) would return 8.

In your case, the array is being passed into the function. If you write function printInstructions(arr) { then your function will automatically assign those steps to arr.

I guess I don’t get how the function will automatically assign those steps if they aren’t defined in the actual code.

The function takes those steps in as a variable.

function printInstructions(arr) { assigns whatever value you input to arr.
So if I run function([“Remove the plastic wrapper”, “Microwave 45 seconds”, “Let cool”, “Enjoy!”, ]), then it automatically does arr=[“Remove the plastic wrapper”, “Microwave 45 seconds”, “Let cool”, “Enjoy!”, ]

I tried that but it says the function didn’t work

Tried what?

What does your code look like right now?

I tried assigning the values to the array. but it didn’t work when I ran it

function printInstructions(array) {
  console.log(array);

  for (let i = 0; i < array[i]; i++) {
    console.log("Step 1 - " + array);
  }
}

Okay, you’re making progress!
Let’s look at your for (let i = 0; i < array[i]; i++) line.
A for statement sets up conditions for a loop of commands. Your conditions are as follows:

  • The first condition establishes i = 0. This is fairly common in for loops, and yours is correct.
  • The second condition tells the loop when to run and stop running. You’ve set this to i < array[i]. This does not work, because you are asking the loop to run when 0 < “Remove the plastic wrapper”, as an example.
  • The third condition establishes actions that run after a loop completes. i++ increases the value of i, and this is correct.

It is only your i < array[i] that is incorrect on this line.

I’m used to seeing it as i < array.length[i]…

Closer! Think about what array.length means, and what would array.length[i] mean?

Also is the assignment, it says Hint : At each step, the counter variable is one less than the step number. That is throwing me off too.