Skip to content

Instantly share code, notes, and snippets.

View appoll's full-sized avatar

Paul Anton appoll

  • Hamburg, Germany
View GitHub Profile
// 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>
// 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;
// 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");
}
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]);
}
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);
}
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!");
}
}
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:
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?
@appoll
appoll / e3.js
Created November 25, 2020 19:21
let books = [
{
pages: 200,
year: 2020,
author: {
firstName: "Paul",
lastName: "Anton"
},
language: "DE"
@appoll
appoll / e3.js
Created November 25, 2020 19:20
let books = [
{
pages: 200,
year: 2020,
author: {
firstName: "Paul",
lastName: "Anton"
},
language: "DE"