# printInstructions function

**URL:** <https://forum.freecodecamp.org/t/printinstructions-function/381673>\
**Category:** General\
**Created:** [April 30, 2020, 9:33pm UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673 "2020-04-30T21:33:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [April 30, 2020, 9:33pm UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/1 "2020-04-30T21:33:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [April 30, 2020, 9:40pm UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/2 "2020-04-30T21:40:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 3:21am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/3 "2020-05-01T03:21:09Z")

</div>

```auto
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.

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 3:27am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/4 "2020-05-01T03:27:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 3:31am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/5 "2020-05-01T03:31:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 3:41am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/6 "2020-05-01T03:41:17Z")

</div>

Okay, let me give you a couple of tips 🙂  
`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?

---

<div class="post-metadata">

**Author:** ![Lninn](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lninn/32/108263_2.png) [@Lninn](https://forum.freecodecamp.org/u/Lninn)\
**Post date:** [May 1, 2020, 4:11am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/7 "2020-05-01T04:11:28Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 4:20am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/8 "2020-05-01T04:20:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 4:21am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/9 "2020-05-01T04:21:07Z")

</div>

This is all still new to me.

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 4:22am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/10 "2020-05-01T04:22:48Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 5:24am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/11 "2020-05-01T05:24:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 5:26am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/12 "2020-05-01T05:26:44Z")

</div>

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!”,]`

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 5:29am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/13 "2020-05-01T05:29:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 5:33am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/14 "2020-05-01T05:33:06Z")

</div>

Tried what?

What does your code look like right now?

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 5:38am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/15 "2020-05-01T05:38:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 5:39am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/16 "2020-05-01T05:39:42Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 5:44am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/17 "2020-05-01T05:44:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 5:46am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/18 "2020-05-01T05:46:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 1, 2020, 5:47am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/19 "2020-05-01T05:47:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paula.Ryal](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paula.ryal/32/145701_2.png) [@Paula.Ryal](https://forum.freecodecamp.org/u/Paula.Ryal)\
**Post date:** [May 1, 2020, 5:47am UTC](https://forum.freecodecamp.org/t/printinstructions-function/381673/20 "2020-05-01T05:47:31Z")

</div>

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.

[Next page](https://forum.freecodecamp.org/t/printinstructions-function/381673.md?page=2)
