Javascript - Objects
Learning goals
What is an object
Accessing data using a key
Changing data using a key
Array of objects
Teacher instruction
Fedt at der var så mange til martins oplæg! Det er altid rart at der kommer studerende når man inviterer folk ind.
Context for where in js we are
Basics
DOM
API
Visualisation
API
Codelab. Bruger i det? Hvorfor, hvorfor ikke?
Divide your problem into smaller problems first
Javascript Test
Peer instruction 1 min
Question 1
What is the value of orResult?
truefalsetrue&&falseNone of the above
Question 2
What will the following code log out?
43
"43"
"age"
"User.age"
Syntax error
Flipped classroom videos
What is an object?
What problem does obejcts solve?
Let's say we wanted to store the name, age and email of a user with what we have learned so far. We could fx create 3 variables to hold this information:
This would work but we would very quickly run into problems when we get more users. What if we had 1000 users, now we would need to create 3000 variables 😱. That would obviously not work.
What about storing this data (name, age and email) in an array. Let's try that:
This is a bit better but what if we forgot where we stored the different values. Let's say we have 30 things we want to store on a user. Was the favorite food stored in index 27 or 28? Or what if we forgot where we stored these things? Now we would have to create an extra array that keeps that of where we store what information
Hmm now we are creating extra work and extra confusion. What if there was a data structure where we save information using a the name what we are storing (key).
Well we are in luck 👇
Objects
In an object we instead use a key to refer to some data. Objects are a key-value pair that can store any data type, including other objects. They're used to store data about a single thing and are often used to store data about a user or a product. Let's solve the problem we outlined above:
Here we create a new object with 3 keys: name, age and email. What is smart is that it is easy to access the kind of data we are interested in.
Accessing data using key
Let's try and access some of the data form the user object
We can access data at a key using either dot notation of square brackets. This is the same: console.log(user.email) and console.log(user['email']).
Changing data using key
We are not only able to access data using key, but we can also change it.
Array of objects
When working with data in javascript it is often in the form of an array of objects. Take a look at this json file that contain movie data. It is an array of objects. Let's take a closer look at it 🔎
Every object in the array represents a movie and has the following keys: title, year, rating, votes, running_times. If i wanted to get the second movies rating in javascript i would write
Exercises
📝 Exercise 1 - level 1
The objects below have some syntax issues - try and fix them all!
📝 Exercise 2 - level 1
Create an object that describes you. Fx a key called age with the value of 28. Add a key that holds an array. Add minimum 6 keys
📝 Exercise 3 - level 1
Log the values of each property of kitten
📝 Exercise 4 - level 1
Write code in the space provided so that the expected values output
Exercises taken from here: https://syllabus.codeyourfuture.io/js-core-2/week-1/lesson
📝 Exercise 5 - level 2
The following object represents all the people that are in space right now.
Data is taken from here
Using the astronautsInSpace variable log out the following things
The number of astronauts in space right now
The name of the craft of the last person in the array
The lastname of the first astronaut in the ISS
📝 Exercise 6 - Lets analyze this code in class
What does it do?
How od we communicate about code?
How should we improve it?
📝 Exercise 7 - Please improve the following code
Algebraic notation chess svær opgave
Lav en funktion der kan tage imod en chess notation og vurdere om den er valid.
https://www.youtube.com/watch?v=iDnW0WiCqNc
https://www.chess.com/terms/chess-notation
📝 Handin - Individual
1 - What to wear
Create a function (that you have to name) that has temperature as parameter. Based on the temperature it should return a string with what the user should wear. You decide what the user should wear based on the temperature.
An example is:

2 - Dice game 🎲
Write a function that simulates a dice roll. You call the function with the number of times you would like to roll the dice.
Every time the dice hits a 6 log out You just hit 6!
Part 2
If the user hits 6 in every roll the log out Jackpot 🎉
3 - Build a sentiment analyzer
A sentiment analyzer is some functionality that figures out how positive/negative a sentence is.
Fx the sentence "I am mega super awesome happy" Should have a high score The sentence "I hate doing boring stuff" should have a low score.
Create a function that takes a string as a parameter. Calling the function will return an object with score, positiveWords and negativeWords. You decide how the score should be implemented and what words are negative and positive.
Here is an example of using the function:
4 - Character frequencies - optional
Write a function that counts the frequency of characters in a string:
5 - Credit card number formatter - optional
This is a very real world example of a problem i got at my previous work. I was tasked to implement one of the smart credit card input fields, where the credit card numbers are seperated with a space. Fx inputting 123456789 would show 1234 5678 9.

Create a function that takes a number as parameter. The function should return the following object:
Things to consider:
What should happen if the function is called with an argument that is not a number?
Handin
Handin on Fronter here. The handin is the 10-10-2022 23:59
Handin the link for the code on GitHub!
When the handin date has passed you will be assigned a classmate to review
Review metrics:
Nævn en ting der inspirerede dig ved opgaven
Find et eksempel på en variabel der har et godt navn og et der har et dårligt navn. Begrund hvorfor
Vælg en af opgaverne og besvar om der var en tydelig og god struktur i koden? Begrund hvorfor/hvorfor ikke
Last updated