"use strict";
//Commentaar
// document.write(5 + 6);
console.log(5 + 6);
console.log("John" + " " + "Doe");

// Declare x, give it the value of 5
var x = 5;  
// Declare y, give it the value of x + 2
var y = x + 2; 
console.log(y);

var x = 5;
console.log(x = x + 5);

var x = "5" + 2 + 3;
console.log(x);

var x = 2 + 3 + "5";
console.log(x);

// 1 tot en met 10
for (let i = 1; i < 11; i++) {
  console.log(i);
}

const PI = 3.14159265359;
console.log(3 * PI);

// You can create a const object:
const car = {type:"Fiat", model:"500", color:"white"};
// You can change a property:
car.color = "red";
// You can add a property:
car.owner = "Johnson";
console.log(car);

// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];
// You can change an element:
cars[0] = "Toyota";
// You can add an element:
cars.push("Audi");
console.log(cars);

// x += y	hetzelfde als x = x + y
// x -= y	hetzelfde als x = x - y
var x = 10;
x += 5;
console.log(x);

var txt1 = "John";
var txt2 = "Doe";
var txt3 = txt1 + " " + txt2;
console.log(txt3);

/*
==	equal to
===	equal value and equal type
!=	not equal
&&	logical and
||	logical or
!	logical not
*/

// The modulus operator returns the division remainder.
var x = 5;
var y = 2;
var z = x % y;
console.log(z);

var x = 5;
x--;
var z = x;
console.log(z);

var length = 16;                               // Number
var lastName = "Johnson";                      // String
var x = {firstName:"John", lastName:"Doe"};    // Object

var x1 = 34.00;     // Written with decimals
var x2 = 34;        // Written without decimals

var x = 5;
var y = 5;
var z = 6;
// (x == y)       // Returns true
// (x == z)       // Returns false

// Array
var cars2 = ["Saab", "Volvo", "BMW"];
// Object
var person2 = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

function myFunction(a, b) {
  return a * b;             // Function returns the product of a and b
}
var x = myFunction(4, 3);   // Function is called, return value will end up in x
console.log(x);

function toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit-32);
}
var y = toCelsius(69);
console.log(y);
var x = toCelsius(77);
var text = "The temperature is " + x + " Celsius";
console.log(text);

var auto = {type:"Fiat", model:"500", color:"white"};
console.log(auto);
console.log(auto.color);

var persoon = {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};
console.log(persoon.fullName());

// When a JavaScript variable is declared with the keyword "new", the variable is created as an object

var answer1 = "It's alright";
var answer2 = "He is called 'Johnny'";
var answer3 = 'He is called "Johnny"';
console.log(answer1);
console.log(answer2);
console.log(answer3);

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
console.log("Lengte: " + txt.length);

var x = "We are \'the\' so-called \"Vikings\" from the \\ north.";
console.log(x);

/*
\n	New Line
\r	Carriage Return
\t	Horizontal Tabulator
*/

var str = "Please locate where 'locate' occurs!";
var pos1 = str.indexOf("locate");
var pos2 = str.lastIndexOf("John");
console.log(pos1);
console.log(pos2);

var q = "abcde";
var pos3 = str.lastIndexOf("a", 5);
console.log(pos3);

var str = "Please locate where 'locate' occurs!";
var pos4 = str.search("u");
console.log(pos4);

var str = "Apple, Banana, Kiwi";
var res1 = str.slice(7, 13);
console.log(res1);
var res2 = str.slice(7);
console.log(res2);
var res3 = str.substring(7, 13);
console.log(res3);
var res4 = str.substr(7, 6);
console.log(res4);
var res5 = str.substr(-4);
console.log(res5);
var n = str.replace("Apple", "Mango");
console.log(n);

var text1 = "Dit is een voorbeeld";
var text2 = text1.toUpperCase();
console.log(text2);
var text3 = text1.toLowerCase();
console.log(text3);
var text10 = "Hello";
var text20 = "World";
var text30 = text10.concat(" ", text20);
console.log(text30);
var str11 = "       Hello World!        ";
console.log(str11.trim());
let str33 = "5";
var str34 = str33.padStart(4,0);
console.log(str34);
// result is 0005
let str35 = "5";
var str36 = str35.padEnd(4,0);
console.log(str36);
// result is 5000
var txt100 = "a,b,c,d,e";   // String
console.log(txt100.split(",")); // Split on commas
var txt101 = "Hello";       // String
console.log(txt101.split(""));           // Split in characters

var x = (0.2 * 10 + 0.1 * 10) / 10; 
console.log(x);

var x = 10;
var y = 20;
var z = "The result is: " + x + y;
console.log(z);

console.log(parseFloat("10.33"));
console.log(parseInt("10.33"));

// JavaScript arrays are used to store multiple values in a single variable.
var cars1 = ["Saab", "Volvo", "BMW"];
cars1[0] = "Opel";
console.log(cars1);
var x = cars1.length;   // The length property returns the number of elements
console.log(x);
var y = cars1.sort();   // The sort() method sorts arrays
console.log(y);
// Accessing the Last Array Element
var last = cars1[cars1.length - 1];
console.log(last);

var fruits, text, fLen, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fLen = fruits.length;

text = "<ul>";
for (i = 0; i < fLen; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";
console.log(text);

var fruits, text;
fruits = ["Banana", "Orange", "Apple", "Mango"];

text = "<ul>";
fruits.forEach(myFunction);
text += "</ul>";

function myFunction(value) {
  text += "<li>" + value + "</li>";
}
console.log(text);

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon");    // adds a new element (Lemon) to fruits
console.log(fruits);

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[fruits.length] = "Lemon";    // adds a new element (Lemon) to fruits
console.log(fruits);

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[4] = "Lemon";    // adds a new element (Lemon) to fruits
console.log(fruits);

var person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
var x = person.length;     // person.length will return 3
var y = person[0];         // person[0] will return "John"

// You should use objects when you want the element names to be strings (text).
// You should use arrays when you want the element names to be numbers.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.toString();
var y = fruits.join(", ");
console.log(x);
console.log(y);

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.pop();      // the value of x is "Mango"
console.log(x);
fruits.push("Kiwi"); 
console.log(fruits);

/*
The first parameter (2) defines the position where new elements should be added (spliced in).
The second parameter (0) defines how many elements should be removed.
The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.
*/
fruits.splice(2, 0, "Lemon", "Kiwi");
console.log(fruits);
fruits.splice(0, 1);        // Removes the first element of fruits
console.log(fruits);

var arr1 = ["Cecilie", "Lone"];
var arr2 = ["Emil", "Tobias", "Linus"];
var arr3 = ["Robin", "Morgan"];
var myChildren = arr1.concat(arr2, arr3);   // Concatenates arr1 with arr2 and arr3
console.log(myChildren);

// The slice() method creates a new array. It does not remove any elements from the source array.
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
// The method then selects elements from the start argument, and up to (but not including) the end argument.
var citrus = fruits.slice(1, 3);
console.log(fruits);
console.log(citrus);

fruits.sort();        // Sorts the elements of fruits
console.log(fruits);
fruits.reverse();     // Then reverse the order of the elements
console.log(fruits);

// If numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
// Ascending
var points = [40, 100, 1, 5, 25, 10];
var x = points.sort(function(a, b){return a - b});
console.log(x);

// Descending
var points = [40, 100, 1, 5, 25, 10];
var y = points.sort(function(a, b){return b - a});
console.log(y);

// Sorting an Array in Random Order
var points = [40, 100, 1, 5, 25, 10];
var z = points.sort(function(a, b){return 0.5 - Math.random()});
console.log(z);

// The Fisher Yates shuffle
var points = [40, 100, 1, 5, 25, 10];
var j, k;
for (i = points.length -1; i > 0; i--) {
  j = Math.floor(Math.random() * i)
  k = points[i]
  points[i] = points[j]
  points[j] = k
}
console.log(points);

function myArrayMax(arr) {
  var len = arr.length;
  var max = -Infinity;
  while (len--) {
    if (arr[len] > max) {
      max = arr[len];
    }
  }
  return max;
}
var q = [100, 5, 350, 6, 700];
console.log(myArrayMax(q));

function myArrayMin(arr) {
  var len = arr.length;
  var min = Infinity;
  while (len--) {
    if (arr[len] < min) {
      min = arr[len];
    }
  }
  return min;
}
console.log(myArrayMin(q));

var cars36 = [
  {type:"Volvo", year:2016},
  {type:"Saab", year:2001},
  {type:"BMW", year:2010}
];
var x = cars36.sort(function(a, b){return a.year - b.year});
console.log(x);
var y = cars36.sort(function(a, b){
  var x = a.type.toLowerCase();
  var y = b.type.toLowerCase();
  if (x < y) {return -1;}
  if (x > y) {return 1;}
  return 0;
});
console.log(y);

var numbers1 = [45, 4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);
function myFunction(value, index, array) {
  return value * 2;
}
console.log(numbers1);
console.log(numbers2);

var numbers = [45, 4, 9, 16, 25];
var over18 = numbers.filter(myFunction);
function myFunction(value, index, array) {
  return value > 18;
}
console.log(numbers);
console.log(over18);

var numbers1 = [45, 4, 9, 16, 25];
var sum = numbers1.reduce(myFunction);
function myFunction(total, value, index, array) {
  return total + value;
}
console.log(sum);

var fruits = ["Apple", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");
var b = fruits.indexOf("Kipsate");
console.log(a);
console.log(b);

var d1 = new Date("October 13, 2014 11:13:00");
var d2 = new Date();
console.log(d1);
console.log(d2);
console.log(d2.toString());
console.log(d2.getDate());
console.log(d2.getMonth() + 1);
console.log(d2.getFullYear());
var time = d2.getHours();
console.log(time);
console.log(d2.getMinutes());
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
console.log(days[d2.getDay()]);

var today, someday, text;
today = new Date();
someday = new Date();
someday.setFullYear(2100, 0, 14);

if (someday > today) {
  text = "Today is before January 14, 2100.";
} else {
  text = "Today is after January 14, 2100.";
}
console.log(text);

console.log(Math.round(-4.2));
console.log(Math.ceil(4.2));
console.log(Math.floor(4.9));
console.log(Math.trunc(4.7));
console.log(Math.pow(8, 2));
console.log(Math.sqrt(64));

console.log(Math.floor(Math.random() * 100) + 1);  // returns a random integer from 1 to 100
function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}
console.log(getRndInteger(100, 1000000));  // random number between min and max (both included)

var age = 16;
var voteable = (age < 18) ? "Too young":"Old enough"; // If the variable age is a value below 18, the value of the variable voteable will be "Too young", otherwise the value of voteable will be "Old enough".
console.log(voteable);
age = "abc";
age = Number(age);
if (isNaN(age)) {
  voteable = "Input is not a number";
} else {
  voteable = (age < 18) ? "Too young" : "Old enough";
}
console.log(voteable);

var greeting;
if (time < 10) {
  greeting = "Good morning";
} else if (time < 20) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}
console.log(greeting);

var day;
switch (new Date().getDay()) {
  case 0:
    day = "Sunday";
    break;
  case 1:
    day = "Monday";
    break;
  case 2:
     day = "Tuesday";
    break;
  case 3:
    day = "Wednesday";
    break;
  case 4:
    day = "Thursday";
    break;
  case 5:
    day = "Friday";
    break;
  case 6:
    day = "Saturday";
}
console.log(day);

switch (new Date().getDay()) {
  case 4:
  case 5:
    text = "Soon it is Weekend";
    break;
  case 0:
  case 6:
    text = "It is Weekend";
    break;
  default:
    text = "Looking forward to the Weekend";
}
console.log(day);

var cars123 = ["BMW", "Volvo", "Saab", "Ford", "Fiat", "Audi"];
var text = "";
var i;
for (i = 0; i < cars123.length; i++) {
  text += "<li>" + cars123[i] + "</li>";
}
console.log(text);

var len;
for (i = 0, len = cars.length, text = ""; i < len; i++) {
  text += cars[i] + "<br>";
}
console.log(text);

var person = {fname:"John", lname:"Doe", age:25};
var text = "";
var x;
for (x in person) {
  text += person[x] + ", ";
}
console.log(text);

var numbers = [45, 4, 9, 16, 25];
var txt = "";
var x;
for (x in numbers) {
  txt += numbers[x] + "<br>";
}
console.log(txt);

var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);

function myFunction(value, index, array) {
  txt = txt + value + "<br>";
}
console.log(txt);

let cars456 = ["BMW", "Volvo", "Mini"];
let text31 = "";
for (let x of cars456) {
  text31 += x + "<br>";
}
console.log(text31);

let language = "JavaScript";
let text32 = "";
for (let x of language) {
text32 += x + "<br>";
}
console.log(text32);

i = 1
var text33 = "";
while (i < 11) {
  text33 += "The number is " + i + "<br>";
  i++;
}
document.write(text33);
console.log(text33);

i = 1;
text = "";
do {
  text += "The number is " + i;
  i++;
}
while (i < 10);
console.log(text);

var cars789 = ["BMW", "Volvo", "Saab", "Ford"];
var i = 0;
var text = "";
for (;cars789[i];) {
  text += cars789[i] + "<br>";
  i++;
}
console.log(text);

var cars890 = ["BMW", "Volvo", "Saab", "Ford"];
var i = 0;
var text = "";
while (cars890[i]) {
  text += cars890[i] + "<br>";
  i++;
}
console.log(text);

text = "";
for (i = 0; i < 10; i++) {
  if (i === 3) { break; }
  text += "The number is " + i + "<br>";
}
console.log(text);

text = "";
for (i = 0; i < 10; i++) {
  if (i === 3) { continue; }
  text += "The number is " + i + "<br>";
}
console.log(text);

console.log(String(100 + 23));
console.log((100 + 23).toString());
console.log(Date().toString());

var patt = /^[1-9][0-9]{3}(?:\s?(?!sa|sd|ss)[a-z]{2})?$/i;
console.log(patt.test("6229 VM"));
console.log(patt.test("6229 SS"));
console.log(patt.test("6229 99"));
console.log(patt.test("ABCD 12"));

function myFunction() {
  var x;
  x = 12;
  try {
    if(x == "") throw "is empty";
    if(isNaN(x)) throw "is not a number";
    x = Number(x);
    if(x > 10) throw "is too high";
    if(x < 5) throw "is too low";
  }
  catch(err) {
    console.log("Error: " + err + ".");
  }
  finally {
    x = "";
  }
}
console.log(myFunction());

var person1 = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
var person2 = {
  firstName:"John",
  lastName: "Doe",
}
var x = person1.fullName.call(person2);  // Will return "John Doe"
console.log(x);

var hello = function() {
  return "Hello World!";
}
console.log(hello);
console.log(hello());

// Use the keyword class to create a class.
// Always add a method named constructor()
class Car {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }
  age() {
    let date = new Date();
    return date.getFullYear() - this.year;
  }  
}
let myCar1 = new Car("Ford", 2014);
let myCar2 = new Car("Audi", 2019);
console.log(myCar1);
console.log(myCar2);
console.log("My car is " + myCar1.age() + " years old.");

class Car2 {
  constructor(name, year) {
    this.name = name;
    this.year = year;
  }
  age(x) {
    return x - this.year;
  }
}
let date = new Date();
let year = date.getFullYear();
let myCar = new Car2("Skoda", 1987);
console.log("My car is " + myCar.age(year) + " years old.");

var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj = JSON.parse(text);
console.log(obj.employees[1].firstName + " " + obj.employees[1].lastName);
