fullstack_webdev

Code and notes from Full stack web developer path, LinkedIn Learning.

View on GitHub

08. Functions and Methods

08_01 The real-world function

08_02 Functions and Methods

// Function declaration

function doSomeMath(a, b) {
  let c = a + b;
  return c;
}
// function expression
const doMoreMath = function (a, b) {
  let c = a * b;
  return c;
};
// IIFE
(function () {
  let a = 4;
  let b = 6;
  let c = doSomeMath(a, b);
  console.log(`The sum of a and b is: ${c}`);
})();

08_03 A Standard Function

const greenPack = {
  name: "Frog Pack",
  color: "green",
  volume: 8,
  pocketNum: 3,
};

// currentPack param going through the entire function
const addPack = function (currentPack) {
  const newArticle = document.createElement("article");
  newArticle.innerHTML = `
  <h1>${currentPack.name}</h1>
  <ul>
    <li>Volume: ${currentPack.volume}</li>
    <li>Color: ${currentPack.color}</li>
    <li>Number of Pockets: ${currentPack.pocketNum}</li>
  </ul>
  `;
  return newArticle;
};

const body = document.querySelector("body");
body.append(addPack(greenPack));

08_04 The Arrow Function 🏹🏹

// Traditional function

function (a){
  return a+100
}

// 1. Valid Arrow function - remove the word "function" and place arrow between the parameters and opening body bracket

(a) => {
  return a+100;
}

// 2. Also a valid arrow function - Remove the body brackets {} and word "return" -- the return is implied

(a) => a + 100;

// 3. Also a valid arrow function - Remove the argument () parentheses

a => a + 100;

08_05 Arrow functions and “this”


08_06 Practice: Build a function


08_07 Pass data to a function with parameters


08_08 Return values from a function


08_09 Practice: Passing values between functions


08_10 Callbacks


08_11 Conditional if…else statement


08_12 Logical Operators

// if statement
if (everydayPack.backpackAge() > 30) {
  console.log("Backpack is used.");
} else {
  console.log("Backpack is new.");
}
// logical statement - and
if (bag.volume > 15 && bag.pockets > 8) {
  console.log("Bag is big");
} else {
  console.log("Bag is small");
}

// logical statement - or
if (bag.volume > 15 || bag.pockets > 8) {
  console.log("Bag is big");
} else {
  console.log("Bag is small");
}

08_13 Conditional Switch Statement

const expr = "Papayas";
switch (expr) {
  case "Oranges":
    console.log("Oranges are $0.59 a pound.");
    break;
  case "Mangoes":
  case "Papayas":
    console.log("Mangoes and papayas are $2.79 a pound.");
    // expected output: "Mangoes and papayas are $2.79 a pound."
    break;
  default:
    console.log(`Sorry, we are out of ${expr}.`);
}
const usedStatus = () => {
  let age = bag.Age();
  let desc;
  switch (true) {
    case age < 30:
      desc = "new";
      break;
    case age >= 30 && age < 365:
      desc = "Lightly used";
      break;
    case age >= 365 && age < 1095:
      desc = "used";
      break;
    case age > 1095:
      desc = "old";
      break;
    default:
      console.log(`There is no description for ${age}.`);
  }
  console.log(`
  Age: ${age} days
  Status: ${desc}
  `);
};

usedStatus();

08_14 Looping Through Content


08_15 Using the map() array method


08_16 Challenge: Build an adavanced function