Code for exercise 1 under "the break keyword" - am I doing something wrong?


#1

The prompt: " Log each element from rapperArray in a for loop with the iterator variable rapperArrayIndex ."

The code I wrote is:

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

// Write you code below
for (rapperArrayIndex = 0; rapperArrayIndex < (rapperArray.length); rapperArrayIndex ++) {console.log(rapperArray[rapperArrayIndex])}

This returns what I think the expected result should be to the console:

Lil’ Kim
Jay-Z
Notorious B.I.G.
Tupac

However the evaluator does not mark the task as complete or give me a “next” button to progress to the next problem. What am I doing wrong?


#2

Please post a link to the exercise so we can test your code and check it against the instructions.


#3

The example is here, many thanks for looking at it.


#4

Don’t forget to complete step 2.


#5

Thank you for the reply. I’m sorry if I’m being dense here, but, on my understanding the issue starts before progressing to step 2. In all the previous exercises you have had to submit the partial answer to move on to the next step (or submit the solution to all steps at once). Is the suggestion that I should have done all three parts at once and submitted them all together? If so ignore the following.

Step 1 reads:

Log each element from rapperArray in a for loop with the iterator variable rapperArrayIndex .

Step 2 reads:

After the for loop, log the string "And if you don't know, now you know." to the console. Note: since there’s a single quote character, ' , in our string, we can use double quotes around the string to make sure character prints.

When I submitted the code:

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

// Write you code below
for (rapperArrayIndex = 0; rapperArrayIndex < (rapperArray.length); rapperArrayIndex ++) {console.log(rapperArray[rapperArrayIndex])}

as the answer/submission to step 1, the automatic evaluator did not allow me to progress to step 2 upon running that code. However it seemed to me to achieve the result the question was asking for, i.e., logging the following output:

Lil’ Kim
Jay-Z
Notorious B.I.G.
Tupac

The code I would want to submit for all three parts would be as follows:

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

// Write you code below

for (rapperArrayIndex = 0; rapperArrayIndex < (rapperArray.length); rapperArrayIndex ++) {console.log(rapperArray[rapperArrayIndex])
if (rapperArray[rapperArrayIndex] === ‘Notorious B.I.G.’) {
break;
} }
console.log (“And if you didn’t know, now you know”)

This seems to log the expected result so I am at a loss as to why the automatic evaluator did not accept that input.


#6

I agree the steps should be done in progression, and each step passed. Usually the next steps ar grayed out until we get a check mark on the previous one. Have you not got the checkmark for step 1?

That does not need to be in parens. It is just a value.


#7

Hi @systemrockstar07137,

If we go back to the syntax used in for loops, we can see that the rapperArrayIndex is the iterator variable.

Take a look at your code and make sure you have the right syntax for all of the elements of the for loop:

  1. an initialization starts the loop and can also be used to declare the iterator variable.
  2. a stopping condition is the condition that the iterator variable is evaluated against— if the condition evaluates to true the code block will run, and if it evaluates to false the code will stop.
  3. an iteration statement is used to update the iterator variable on each loop.

#8

I had the same problem but the issue got fixed when I changed my variable from ‘rapperArrayIndex’ to ‘i’.
My code worked fine but I didn’t get the tick until I did that.


#9

Belated thanks, this was it.


#10

Yep. Instructions want us to use an iterator variable of “rapperArrayIndex,” but you can’t get the exercise “right” unless your variable is “i.” Frustrating.


#11

Agree with all these statements. I was completely confused by the instructions : " Log each element from rapperArray in a for loop with the iterator variable rapperArrayIndex << << because of the camelcase format its either highly misleading or an intentional trick statement. I hate that i can’t get the answer to just one step of a challenge because if I can’t figure out step 1 and ask for the solution I don’t get to even attempt the other steps. :angry:


#12
const rapperArray = ["Lil' Kim", "Jay-Z", "Notorious B.I.G.", "Tupac"];

// Write you code below
for (rapperArrayIndex = 0;  rapperArrayIndex < rapperArray.length; rapperArrayIndex++) {
  console.log(rapperArray[rapperArrayIndex]);
  if (rapperArray[rapperArrayIndex] === 'Notorious B.I.G.') {
    break;
  }
}
console.log("And if you don't know, now you know.");

It should come as neither misleading or an intentional trick. It is the way a lot of JavaScript variables are written by convention.

rapper array        =>  rapperArray

rapper array index  =>  rapperArrayIndex

We cannot use spaces in our variable names so collapse around spaces and Capitalize the second through subsequent words in the name. The first letter is never capitalized.


#13

Correct code:

const rapperArray = [“Lil’ Kim”, “Jay-Z”, “Notorious B.I.G.”, “Tupac”];

// Write you code below

for (let rapperArrayIndex = 0; rapperArrayIndex < rapperArray.length; rapperArrayIndex++)
{
console.log(rapperArray[rapperArrayIndex]);
}

I had the same problem. It turned out that I did not give a variable (let)… :disappointed:


#14

let is a keyword for declaring block scope. You’ll notice that if we leave it out, the code still works as expected but the variables are still accessible after the loop is complete. let prevents this.

The requirements of the exercise must have included this, which hints to the possibility it is in the instructions (which it is not). It is in the example given in the lesson text, though.


#15

On the same exercise, I wrote:
for (i = 0; i < rapperArray.length; i++){
console.log(rapperArray[i]);
}
which returned
Lil’ Kim
Jay-Z
Notorious B.I.G.
Tupac

and as with original poster, my code is not recognized as correct - step 2 remains greyed out. I have tried substituting rapperArrayIndex for i … and that didn’t work either?

in my case, when i added “let” as in let i = …
i believe that made the code pass, though it’s hard to tell, because i called for the solution code and am having trouble re-setting exercise.


#16

which would indicate the SCT is looking for the let keyword. Bear in mind that undeclared variables are automatically hoisted to the top of global scope and may result in collisions. Make it a rule to never write functions without explicit variable declarations. var for variables that may change inside the function, let for variables that exist only inside loops, and const for variables that do not or should not change.


#17

Thank you, that helps a lot!