Skip to content

Conversation

@HomamS
Copy link

@HomamS HomamS commented Aug 24, 2019

No description provided.

//10.I’ve declared a multidimensional array (an array inside an array) -- see below. How can I access the third item’s second element? i.e. [2, 1] is the third element, and I want to access 1. Add a console.log statement accessing this item.

const grid= [[0,1], [1,1], [2,1], [3,1]];
const item= grid[2,2];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't cover this in class, but when working with nested arrays (arrays inside arrays), you need to use two sets of square brackets to access the element like grid[2][1].

For example, if you use grid[2], you get the second element of the grid array [2, 1]. So if you use grid[2][1], you get the second element of grid[2], which is 1.

grid[2]; // this is [2, 1]
grid[2][1]; // this is 1

I see why you'd use a comma like in [2, 2]! But that actually prints out [2, 1] instead of 1. I didn't understand why until I realized the comma operator returns the last value, so grid[2,2] becomes [2] (explanation)

Copy link
Collaborator

@epq epq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!

console.log('The array has no value becauswe its empty');
console.log(list);

const favouritanimal = ['Cat','Dog','Birds','elephant'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the instructions said to use a plural name for your array since it contains a list of items, so a better name would be favouriteAnimals

}
//6- Arrays//

const list = ['','',''];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The array here isn't empty - it does have values even though they're empty strings.
An empty array would be const list = [];

//8.If x equals 7, and the only other statement is x = x % 3, what would be the new value of x?

var x=7;
x = x%7;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused about what you're testing in this question! note that this should be x = x % 3, not x = x % 7


//12.12. Here’s a profile about a cat for adoption. Create variables to hold information about this cat as shown on the profile. For example:

const namee = 'Prince';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you declared name and age above so you can't redeclare them again, but I think catName and catAge would be better names to use in this case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants