Basic Data Structures Flashcards

Cards, resources and exercises of Data Structures in Javascript

1
Q

What Are Basic data structures in Javascript ?

A

Variable, Arrays and Objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to add item at the beginning of an array ?

A

let arr = [1,2,3,4];

arr.unshift(0);

//result 0,1,2,3,4

Obs.: unshift method accepts one or more parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to add item at the end of an array ?

A

let arr = [0 ,1,2,3,4];

arr.push(5);

//result 0,1,2,3,4,5

Obs.: push method accepts one or more parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to remove item at the beginning of an array ?

A

let arr = [0,1,2,3,4,5];

arr.shift();

//result 1,2,3,4,5

Obs.: doens’t accepts parameters
Obs.: returns element removed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to remove item at the end of an array ?

A

let arr = [0,1,2,3,4,5];

arr.pop();

//result 0,1,2,3,4

Obs.: doens’t accepts parameters
Obs.: returns element removed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to remove itens at the middle of an array ?

A
let arr = [2, 5, 1, 5, 2, 1];
  let removed = arr.splice(2, 2);
 // sum elements
  arr.reduce((a, b) => a + b);

//result 10

Obs.: returns an array with elements removed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to change values of an array using splice ?

A

let arr = [“eu”, “gosto”, “de”, “praia”];

arr.splice(3, 1, “parque”);

//results [“eu”, “gosto”, “de”, “parque”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to retrieve a slice of an array ?

A

let animals = [“dog”, “cat”, “giraffe”, “crocodile”, “elephant”];

let pets = animals .slice(0,2);

//results pets ==> ["dog","cat"]
// results ==> animals ["dog", "cat", "giraffe", "crocodile", "elephant"]

Obs.: slice doesn’t change the source array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to copy an entire array without ref and without change it?

A

let jsDataTypes = [‘boolean’,’undefined’,’number’, ‘null’,’string’, ‘symbol’];

let data = […jsDataTypes];

//results data ==> [‘boolean’,’undefined’,’number’, ‘null’,’string’, ‘symbol’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to Combine Arrays with the Spread Operator ?

A

let japanCarBrand = [‘toyota’,’honda’,’nissan’];
let usaCarBrand = [‘ford’, ‘chevrolet’,’GMC’];
let franceCarBrand = [‘renault’, ‘citroen’,’pegeout’];

let carBrands = […japanCarBrand, …usaCarBrand, …franceCarBrand];

//results [‘toyota’,’honda’,’nissan’,’ford’, ‘chevrolet’,’GMC’,’renault’, ‘citroen’,’pegeout’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to check if an alement exists in the array ?

A

let names = [‘sandra’, ‘julia’, ‘meryl’, ‘angelina’,’scarlet’];

let index = names.indexOf(‘julia’);

//results 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to through all itens of an array with basic for loop ?

A

let decades = [1970,1980,1990,2000,2010];

for (let i = 0; i < decades.length; i++){
    console.log(decades[i]);
}
//results 1970, 1980, 1990, 2000, 2010
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to create complex multi-dimensional arrays ?

A
let myNestedArray = [
  // change code below this line
  [
    [
      'terceiro nível',
      [
        'quarto nível',
        [
          'quinto nível',
          'deepest'
        ],
        'deeper'
      ],
      'deep'
    ]
  ]
];
console.log(myNestedArray[0][0][1][1][1]);
//results deepest
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to create an object in Javascript ?

A
let foods = {
  apples: 25,
  oranges: 32,
  plums: 28
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to add properties to an existing object ?

A
let foods = {
  apples: 25,
  oranges: 32,
  plums: 28
};

//add new properties

foods. bananas = 13;
foods. grapes = 35;
foods. strawberries = 27;

Obs.: If the property doesn’t exist yet, is created with the value passed;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to Modify an Object Nested Within an Object ?

A
let userActivity = {
  id: 23894201352,
  date: 'January 1, 2017',
  data: {
    totalUsers: 51,
    online: 42
  }
};
userActivity.data.online = 45;
17
Q

How to retrieve value from object property accessing by variable ?

A
let foods = {
  apples: 25,
  oranges: 32,
  plums: 28,
  bananas: 13,
  grapes: 35,
  strawberries: 27
};
function checkInventory(scannedItem) {
  // change code below this line
return foods[scannedItem];
}

// returns 25

18
Q

How to remove a property from object ?

A
let foods = {
  apples: 25,
  oranges: 32,
  plums: 28,
  bananas: 13,
  grapes: 35,
  strawberries: 27
};

delete foods.oranges;
delete foods.plums;
delete foods.strawberries;

19
Q

How to check if an object has a property ?

A
let users = {
  Alan: {
    age: 27,
    online: true
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: true
  },
  Ryan: {
    age: 19,
    online: true
  }
};
//using 'in'
function isEveryoneHere(obj) {
return 'Alan', 'Jeff', 'Sarah', 'Ryan' in obj;
}
// results true

or alternatively

//using 'hasOwnProperty'
function isEveryoneHere(obj) {
 return obj.hasOwnProperty('Alan');
}

//returns true

20
Q

How to Iterate Through the Keys of an Object ?

A

Possible Solution

let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};
function countOnline(obj) {
  let total = 0;
  for(let user in obj){ 
    if(obj[user].online){
      total++;
    }
  }
  return total;
}

console.log(countOnline(users));

// results 2

Note: for … in statement brings only the object key. To access the property value it is necessary to pass the object key to retrieve the value of the object.

21
Q

How to all keys from a object ?

A
let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

Object.keys(users);

//results Alan,Jeff,Sarah,Ryan

22
Q

How to add item in the array wihthin object property ?

A
let user = {
  name: 'Kenneth',
  age: 28,
  data: {
    username: 'kennethCodesAllDay',
    joinDate: 'March 26, 2016',
    organization: 'freeCodeCamp',
    friends: [
      'Sam',
      'Kira',
      'Tomo'
    ],
    location: {
      city: 'San Francisco',
      state: 'CA',
      country: 'USA'
    }
  }
};
function addFriend(userObj, friend) {
  userObj.data.friends.push(friend);
  return userObj.data.friends;
}

console.log(addFriend(user, ‘Pete’));

//Results Sam,Kira,Tomo,Pete