Solutions are also available as notebook
Exercise 1 a) Create three variables and assign values with different data types to them
// number, string, boolean
let number = 10
let name = "Rikku"
let likesProgramming = trueExercise 1 b) Print all the variables using JavaScript's built-in function console.log()
console.log(number, name, likesProgramming)Exercise 1 c) Print the data type of each variable using JavaScript's built-in keyword "typeof"
console.log(typeof number) console.log(typeof name) console.log(typeof likesProgramming)Exercise 2 How do we use variables? Explore with arithmetic operators (+, -, *, /)!
// Example
let firstValue = 3
let secondValue = 5 let result = firstValue + secondValue result = firstValue - secondValue result = firstValue * secondValue result = firstValue / secondValue // Example
let veggies = ['orange', 'lime', 'olive', 'tomato', 'plum', 'maroon'] veggies[0] veggies[1]We can pick one or more from the start, and get a new array of the rest
let [firstVeggie, ...otherVeggies] = veggies console.log(firstVeggie)We can also create a slice of the array, specifying where to start and end
veggies.slice(2, 5) veggies.slice(2) veggies.slice(0, 2)Exercise 3 a) Create an array with names of your friends and print the name of the first, second and then the rest
let names = ["Tusse", "Misse", "Nisse", "Lusse"] names[0] names[1] let [,, ...rest] = names console.log(rest)Exercise 3 b) Log your array of names using different slices
names.slice(0,2) names.slice(2) names.slice(1, 3)Exercise 4 a) Count how many names you have added to the a array using array.length
names.lengthExercise 4 b) Change the array, adding a new name first: array = [newItem, ...array]
names = ['Hubbe' , ...names]Exercise 4 c) Change the array, removing two names: [first, second, ...array] = array
[katt1, katt2, ...names] = namesLog the removed names. Does it look as expected?
console.log(katt1, katt2)Exercise 4 d) Remove two names like above, and then ignore them: [, , ...array] = array
[, , ...names] = namesLog the array. Does it look as expected?
console.log(names)Exercise 4 e) Combine two arrays of names, using: [...firstArray, ...secondArray]
let moreNames = ["Murre", "Miso", "Tasse"]
names = [...names, ...moreNames]Log the array. Does it look as expected?
console.log(names) // Example
for (let veggie of veggies) {
console.log(veggie)
} for (let index in veggies) {
veggies[index] = veggies[index] + 's'
}
console.log(veggies)Exercise 5 a) Log each name in your array of names
for (let name of names) {
console.log(name)
}Exercise 5 b) Add a small change to each name in your array
for (let index in names) {
names[index] = names[index] + 's'
}
console.log(names) // Example
let countries = { "sweden" : "stockholm", "great britain" : "london", "japan" : "tokyo" } let capital = countries["sweden"]Exercise 6 a) Get the capital of Great Britain and Japan using the object "countries"
countries["great britain"] countries.japanExercise 6 b) Add a new key-value pair to the object "countries"
countries.taiwan = 'taipei'Exercise 6 c) Update Sweden's capital to Umeå
countries.sweden = 'umeå'Exercise 6 d) Remove the key-value pair Sweden-Stockholm from the object
delete countries.swedenLog the object. Does it look as expected?
console.log(countries)Exercise 6 e) Print each key-value pair in the object countries
for (let [key, value] of Object.entries(countries)) {
console.log("Country: "+key+", Capital: "+value)
} // Example
function myFunction (param1, param2) {
// Block of code
}
(param1, param2) => {
// Block of code
}
// log the name and content
console.log(myFunction.name)
console.log(myFunction.toString()) // arrow functions are anonymous until you assign them to a variable
let arrowFunction = () => {}
arrowFunction.nameWe will start with creating a function that can add two numbers.
function addTwoNumbers (number1, number2) {
let result = number1 + number2
return result
}Let's try to use it!
addTwoNumbers(1, 2) addTwoNumbers(5, 20)Exercise 7 a) Create a function that can calculate the difference between two numbers
function subtract(number1, number2) {
let result = number1 - number2
return result
}Exercise 7 b) Use the function. Does it return the expected results?
subtract(5, 20)Exercise 7 c) Create a function that can multiply two numbers
function multiply(number1, number2) {
result = number1 * number2
return result
}Exercise 7 d) Use the function. Does it return the expected results?
multiply(5, 20)Exercise 7 e) Create a function that can divide two numbers
function divide(number1, number2) {
result = number1 / number2
return result
}Exercise 7 f) Use the function. Does it return the expected results?
divide(5, 20)