This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let books = [ | |
{ | |
pages: 200, | |
year: 2020, | |
author: { | |
firstName: "Paul", | |
lastName: "Anton" | |
}, | |
language: "DE" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
readlineSync = require('readline-sync') | |
console.log("BEFORE") | |
userName = readlineSync.question("What is your name?\n"); | |
console.log("Nice to meet you, " + userName); | |
console.log("AFTER") | |
limit = 1970; | |
// hardcoded value; | |
// how to make this come from the user? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
readlineSync = require('readline-sync') | |
userName = readlineSync.question("What is your name?\n"); | |
console.log(typeof userName); | |
age = readlineSync.question("What is your age?\n"); | |
console.log("The type of age\n"); | |
console.log(typeof age); | |
// Task 1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("before the loop"); | |
// I want to print all integer numbers from 0 to 50; | |
// for (i = 0; i <= 50; i++){ | |
for (i = 0; i <= 50; i = i+1){ | |
console.log(i); | |
if (i===10){ | |
console.log("bingo!"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ages = [12, 16, 24, 98, 64, 24, 98, 64, 24, 98, 64] | |
// Print all the ages in your array :) | |
// We use the for loop | |
for (let i = 0; i < ages.length; i = i + 1){ | |
let currentAge = ages[i]; | |
console.log("Position is: " + i); | |
console.log("Value is: " + currentAge); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ages = [12, 16, 24, 28, 69, 14, 13, 80] | |
// I can copy values from one array into another | |
// Copy all ages which are greater than 18 to a second array | |
largerThan18 = [] | |
for (i=0; i < ages.length; i++){ | |
if (ages[i] > 18){ | |
largerThan18.push(ages[i]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Before you write your own function, answer 2 questions: | |
// 1. What should the function do? (the action) | |
// 2. What does it need? (the parameters) | |
function helloUser(userName, userAge){ | |
console.log("Hello, " + userName); | |
console.log("You are " +userAge+ " years old"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Work with functions that take as parameters arrays (one or more) | |
// E3. Write a function that returns the sum of all elements in an array | |
function addArrayElements(array){ | |
return sum; // number | |
} | |
// E3.a Modify the function to only return the sum of all even elements; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// E5. | |
// Write a function that takes two parameters: an array and a Number; | |
// The function returns the index of | |
// the element's first occurence in the array; | |
// should return position of number in array | |
function findPositionOfNumberInArray(array, number){ | |
// iterate over the array -> for loop | |
// compare the item of each position in the array to the <givenNumber> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let book1 = { | |
pages: 200, | |
year: 2022, | |
authors: | |
[ | |
{ | |
firstName: "Paul", | |
lastName: "Anton" |