Clarifying Object Lookup

I solved the problem, but had a few questions about the solution. First, for the arr.length part, why does that refer to the contents inside the failure array. Shouldn’t it be result.failure.length = 1 because there is one array, and then result.failure[0].length would be 3? Same for the arr[i] part. I know there is only 1 array, but don’t you have to specify which array?

  **Your code so far**
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["no-extra-semi", "no-dup-keys"]
};
function makeList(arr) {
// Only change code below this line
let failureItems = [];
for (let i = 0; i < arr.length; i++) {
  failureItems.push(`<li class="text-warning">${arr[i]}</li>`);
} 
// Only change code above this line

return failureItems;
}

const failuresList = makeList(result.failure);
console.log(makeList(result.failure));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Create Strings using Template Literals

Link to the challenge:

The array is specified here, the function receive only a flat array

This one specifically

So this is arr, arr.length is 3 and arr[0].length is 6

So for 1 array I don’t have to specify that array? Also, if there were 2 arrays in result.failure then would I have to specify which one?

I’m not sure where your confusion is at, but this really has little to do with arrays.

The function makeList() has one parameter, which is named arr in this case. However the name of the parameter is arbitrary, so you could name it anything. The fact that the function expects an array to be passed is also only implied, since you can’t specify parameter types in JavaScript.

Regardless, the call to makeList() at the bottom passes result.failure, which is an array. You could pass any other array you want and it should still work.

Here’s a simpler example:

const x = 40;
const y = 2;

console.log(add(x, y)); // 42

function add(a, b) {
  return a + b;
}

You can see inside the add function definition, it has no idea what x and y are and never references them. However it still returns their sum when they get passed in as arguments.

My question isn’t really in the name of the parameter but more of how to accessing values. I thought that arr.length for result.failure would return 1 b/c there is one array. I’m confused with thinking that the array in result.failure = 1 group. My thought process is result.failure looks at the value of the failure property in the result object. In that failure property the value is 1 array therefore length is 1. If I want to access the data in the array I have to specify that. Sorry if im not making sense, I’m new to this and don’t have much practice articulating my questions.

I don’t know what to tell you other than that your understanding’s off here. result.failure refers to an array with 3 items, so result.failure.length is 3.

const list = [1, 2, 3, 4];
console.log(list.length); // 4

const obj = {
  list: [1, 2, 3, 4]
};
console.log(obj.list.length); // 4
1 Like

What if result.failure had two arrays each with 3 items. Then would the length be 6? I guess a better way of phrasing it is: would arr.length ignore the array brackets or parenthesis and count each value individually regardless of how the values are grouped?

I suggest you just type stuff into the editor and try it out.

const listOfLists = [
  [1, 2, 3, 4],
  ["a", "b", "c"]
];

console.log(listOfLists.length); // 2
console.log(listOfLists[0].length); // 4
console.log(listOfLists[1].length); // 3

Got it! I’ll play around with the editor. Thanks for helping me!

P.S. Your post on recursion really helped me understand it! I was lost before xD.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.