Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[JavaScript] Switch Statement

Note: Case values are tested with strict equality (===)

/* Default Option in Switch Statements */
var result = "";
switch (num) {
  case 1:
    result = "1";
    break;
  case 2:
    result = "2";
    break;
  default:
    result = "3 or more";
    break;
}

/* Multiple Identical Options in Switch Statements */
var result = "";
switch(val) {
  case 1:
  case 2:
  case 3:
    result = "1, 2, or 3";
    break;
  case 4:
    result = "4 alone";
}

[JavaScript] Iterate

While Loops

var myArr = [];
var i = 0;

while(i < 5) {
  myArr.push(i);
  i++;
}

For Loops

var myArr = [];

// for ([initialization]; [condition]; [final-expression])
for (var i = 0; i < 5; i++) {
  myArr.push(i);
}

Iterate through an Array with a For Loop

var myArr = [ 2, 3, 4, 5, 6];
var total = 0;

for (var i = 0; i < myArr.length; i++) {
    total += myArr[i];
}

Nesting For Loops

var myArr = [
  [1,2], [3,4], [5,6]
];

for (var i=0; i < myArr.length; i++) {
  for (var j=0; j < myArr[i].length; j++) {
    console.log(myArr[i][j]);
  }
}

For…In Loops

Iterate through all the keys within an object.

let users = {
  Max: {
    age: 27
  },
  Mira: {
    age: 32
  },
  Rich: {
    age: 48
  }
};

for (let user in users) {
  console.log(user); // logs: Max, Mira, Rich
  if (users[user].age > 40) {
      console.log(`${user} is old.`);
  }
} 

Do…While Loops

A do...while loop ensures that the code inside the loop will run at least once.

var myArr = [];
var i = 0;
do {
  myArr.push(i);
  i++;
} while (i < 5);

Replace Loops using Recursion

/* For Loop */
function multiply(arr, n) {
   var product = 1;
   for (var i = 0; i < n; i++) {
       product *= arr[i];
   }
   return product;
}

/* Replace For Loop with Recursion */
function multiply(arr, n) {
   if (n <= 0) {
     return 1;
   } else {
     return multiply(arr, n - 1) * arr[n - 1];
   }
}

Note: Recursive functions must have a base case when they return without calling the function again (in this example, when n <= 0), otherwise they can never finish executing.

/* Count to n */
function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}

console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]

Note: The push happens last, after the recursive call has returned. Thats why the value of n decreases, but the values in the final array are increasing.

/* Create a Range of Numbers */
function rangeOfNumbers(startNum, endNum) {
  if (startNum == endNum) {
    return [startNum];
  } else if (startNum < endNum) {
    const rangeArray = rangeOfNumbers(startNum + 1, endNum);
    rangeArray.unshift(startNum);
    return rangeArray;
  } 
};

console.log(rangeOfNumbers(5, 10)); // [ 5, 6, 7, 8, 9, 10 ]

[JavaScript] Functions

/* Basic Function */
function reusableFunction() {
  console.log("Hello World");
}
reusableFunction();


/* Passing Values to Functions with Arguments */
function functionWithArgs(arg1, arg2 ) {
    console.log(arg1 + arg2);
}
functionWithArgs(1, 2); //returns 3


/* Global vs. Local Scope in Functions 
It is possible to have both local and global variables with the same name. 
When you do this, the local variable takes precedence over the global variable. */
var outerWear = "T-Shirt";
function myOutfit() {
  var outerWear = "sweater"
  return outerWear;
}
myOutfit(); //returns "sweater"

[JavaScript] Comparsion Operators

/* Equality Operator (type conversion / type coercion) */
1   ==  1   // true
1   ==  2   // false
1   == '1'  // true
"3" ==  3   // true


/* Strict Equality Operator (no type conversion) */
3 ===  3   // true
3 === '3'  // false


/* Inequality Operator (type conversion) */
1 !=  2     // true
1 != "1"    // false
1 != '1'    // false
1 != true   // false
0 != false  // false

/* Strict Inequality Operator (no type conversion) */
3 !==  3   // false
3 !== '3'  // true
4 !==  3   // true

/*Greater Than Operator (type conversion) */
5   >  3   // true
7   > '3'  // true
2   >  3   // false
'1' >  9   // false

/* Greater Than Or Equal To Operator (type conversion) */ 
6   >=  6   // true
7   >= '3'  // true
2   >=  3   // false
'7' >=  9   // false

/* Less Than Operator (type conversion) */
2   < 5  // true
'3' < 7  // true
5   < 5  // false
3   < 2  // false
'8' < 4  // false

/* Less Than Or Equal To Operator (type conversion) */
4   <= 5  // true
'7' <= 7  // true
5   <= 5  // true
3   <= 2  // false
'8' <= 4  // false