Skip to content

Latest commit

 

History

History
115 lines (81 loc) · 2.48 KB

File metadata and controls

115 lines (81 loc) · 2.48 KB

Objects

Learn the Basics

  • Understand why and how objects are used.

  • Objects are also known as dictionaries in Python and Hash Maps in Java.

  • We'll use JavaScript. But you should be able to follow along no matter which programming language you prefer.

JavaScript

Python

  • Google for "dictionaries" and learn the basics

Java

  • Google for "hash maps" and learn the basics

Objects vs Arrays

https://www.youtube.com/watch?v=iiV19OeSqmA

Why use Objects

https://www.youtube.com/watch?v=EnBabBX6JPc

Case Study - Counting Uniques Problem

Code: https://gitlab.com/snippets/1976631

https://www.youtube.com/watch?v=FU4U1hSyZPk

Case Study - Counting Pairs Problem

Code: https://gitlab.com/snippets/1976634

https://www.youtube.com/watch?v=jtSvUofvyRg

Exercises

1. Numbers and squares:

Define a function which returns an object containing the square of each number from 1 to n

function squares(n) {
    // your code here.
}

Example: For n = 5. Your function should return:

{
  "1": 1,
  "2": 4,
  "3": 9,
  "4": 16,
  "5": 25
}

2. Countries and capitals

Consider the below string which contains countries and their capitals in csv format.

`
"country","capital"
"Afghanistan","Kabul"
"Argentina","Buenos Aires"
"Australia","Canberra"
"Austria","Vienna"
"Bangladesh","Dhaka"
"Barbados","Bridgetown"
"Bhutan","Thimphu"
"Brazil","Brasília"
"Canada","Ottawa"
"China","Beijing"
"Denmark","Copenhagen"
"Egypt","Cairo"
"Finland","Helsinki"
"France","Paris"
"Germany","Berlin"
"Greece","Athens"
"India","New Delhi"
"Indonesia","Jakarta"
"Iran","Tehran"
"Iraq","Baghdad"
"Ireland","Dublin"
"Israel","Jerusalem"
"Italy","Rome"
"Japan","Tokyo"

`

Write a function that takes this string as an input argument and returns an object where the keys are the countires and the values are the corresponding capitals. Your output should look something like:

{
  "Afghanistan": "Kabul",
  "Argentina": "Buenos Aires"
  etc...
}