text: const myText = 'I am a string';
  const newString = myText.replace('string', 'developer.');
  console.log(newString);
  
  const myArray = ['I', 'love', 'Igbo', 'recipes.'];
  const madeString = myArray.join(' ');
  
  console.log(madeString);
  
  const madeAnotherString = myArray.join();
  console.log(madeAnotherString);
  
  const myNumber = Math.random(); // Randomly generating numbers from 0 to 1 
  console.log(myNumber);
  
  // function draw() {
  //   ctx.clearRect(0, 0, WIDTH, HEIGHT);
  //   for (let i = 0; i < 100; i++) {
  //     ctx.beginPath();
  //     ctx.fillStyle = 'rgba(225, 0, 0, 0.5)';
  //     ctx.arc(random(WIDTH), random(HEIGHT), random(50), 0, 2 * Math.PI);
  //     ctx.fill();
  //   }
  // }
  
  // console.log( draw() );
  
  function salute() {
    console.log( 'Hello, welcome to CoreJSFunction!' );
  }
  
  salute();
  
  
  function hello(name='Chris') {
    console.log(`Hello ${name}!`);
  }
  
  hello('Arinze');
  hello();
  
  
  // Anonymous and arrow functions
  // function() {
  //   console.log('Hello');
  // }
  
  // function logKey(event) {
  //   console.log(`You pressed "${event.key}".`)
  // }
  
  // textBox.addEventlistener('keydown', logKey);
  
  const textBox = document.querySelector('#textBox');
  const output1 = document.querySelector('#output');
  
  textBox.addEventListener('keydown', event => output1.textContent = `You pressed '${event.key}'.`);
  
  
  const originals = [1, 2, 3];
  const doubled = originals.map(item => item * 5);
  
  console.log(doubled);
  
        // Equivalent to:
  
  function doubleItems(items) {
    return items * 5;
  }
  
  console.log (doubleItems( 2 ));
  console.log (doubleItems( 10 ));
  console.log (doubleItems( 20 ));
  
  
  // FUNCTION SCOPE AND CONFLICTS
  const x = 1;
  
  function a() {
    const y = 2;
    output(y);
  }
  
  function b() {
    const z = 3;
    output(z);
  }
  
  function output(value) {
    const para = document.createElement('p');
    document.body.appendChild(para);
    para.textContent = `Value: ${value}`;
  }
  
  // Functions inside functions
  function myBigFunction() {
    const myValue = 1;
  
    subFunction1(myValue);
    subFunction2(myValue);
    subFunction3(myValue);
  }
  
  function subFunction1(value) {
    console.log( value );
  }
  
  function subFunction2(value) {
    console.log( value );
  }
  
  function subFunction3(value) {
    console.log( value );
  }
  
  myBigFunction();
  
  
  // Function return values
  const input = document.querySelector('.numberInput');
  const para = document.querySelector('p');
  
  function squared(num) {
    return num * num;
  }
  
  function cubed(num) {
    return num * num * num;
  }
  
  function factorial(num) {
    if (num < 0) return undefined;
    if (num == 0) return 1;
    let x = num - 1;
    while (x > 1) {
      num *= x;
      x--;
    }
    return num;
  }
  
  input.addEventListener('change', () => {
    const num = parseFloat(input.value);
    if (isNaN(num)) {
      para.textContent = 'You need to enter a number!';
    } else {
      para.textContent = `${num} squared is ${squared(num)}. `;
      para.textContent += `${num} cubed is ${cubed(num)}. `;
      para.textContent += `${num} factorial is ${factorial(num)}. `;
    }
  });
  
  
  // Outer variables used in functions
  let userName = 'Customer';
  
  function showMessage() {
    let message = 'Hello ' + userName;
    console.log(message);
  }
  
  showMessage();
  
  // Another example
  const netWorth = '$200+ billion';
  
  function salute(user) {
    console.log(`${user}: welcome to CoreJSFunction`);
  }
  
  salute(netWorth);
  
  
  // Parameters
  function showText(from, text) {
    console.log( from + ': ' + text);
  }
  
  showText('Arinzechukwu', 'Welcome!');
  showText('Chidiebere', 'How are you doing!?');
  
  
  function showMessage(from, text1) {
    from = '*' + from + '*';  // Making from look nicer
  
    console.log(from + ': ' + text1);
  }
  
  let from = 'Maazi';
  
  showMessage(from, 'Hello!'); // *Maazi*: Hello!
  
  
  // DEFAULT PARAMETERS
  showMessage('Ngozichukwu'); // *Ngozichukwu*: undefined
  
  function showMessage1(from, text2 = 'No text given.') {
    from = '*' + from + '*';  // Making from look nicer
  
    console.log(from + ': ' + text2);
  }
  
  showMessage1('Chidimma'); // *Chidimma*: No text given.
  
  
  // ALTERNATIVE DEFAULT PARAMETERS
  function displayMessage(text3) {
    if (text3 == undefined) {
      text3 = 'Empty message';
    }
    console.log(text3);
  }
  
  displayMessage();         // Empty message
  displayMessage('Ndidi');  // Ndidi
  
  // Using the coalescing operator
  function showCount(count) {
    // if count is undefined or null, show 'unknown'
    console.log(count ?? 'unknown');
  } 
  
  showCount(0);       // 0
  showCount(null);    // unknown
  showCount();        // unknown
  
  
  // RETURNING A VALUE
  function sum(a, b) {
    let value = a + b;
    return value
  }
  
  console.log( sum(5, 5) );   // 10
  
  // Using many return values in a code
  
  function checkAge(age) {
    if (age >= 18) {
      return true;
    } else {
      return confirm('Do you have permission from your parents?');
    }
  }
  
  let age = prompt('How old are you?', '');
  
  if ( checkAge(age) ) {
    alert('Access granted.');
  } else {
    alert('Access denied.');
  }
  
  
  // Using return without a value
  function showMovie(age) {
    if ( !checkAge(age) ) {
      return;
    }
    alert( 'Showing the movie...' );
  }
  
  
  // FUNCTIONS == COMMENT 
  function showPrimes(n) {
    for (let i = 2; i < n; i++) {
      if ( !isPrime(i)) continue;
  
      console.log(i); 
    }
  }
  
  function isPrime(n) {
    for (let i = 2; i < n; i++) {
      if ( n % i == 0 ) return false;
    }
    return true;
  }
  
  showPrimes(25);
  showPrimes(10);
  isPrime(25);
  isPrime(10);
  
  // Practice
  // function checkDateOfBirth(age1) {
  //   return (age1 > 18) ? true : confirm('Did parents allowed you?');
  // }
  
  
  // Using ||
  
  // function checkAge1(age2) {
  //   return (age2 > 18) || confirm('Were you allowed by your parents?');
  // }
  
  // checkDateOfBirth(4);
  // checkAge1(8);
  
  function pow(x, n) {
    return (x ** n);
  }
  
  function min(a, b) {
    if (a < b) {
      console.log(a);
    } else {
      console.log(b);
    }
  }
  
  // A solution with the question mark operator
  // function min(a, b) {
  //   console.log(a < b) ? a : b;
  // } 
  
  min(2, 5);    // 2
  min(3, -1);   // -1
  min(1, 1);    // 1
  
  function pow(x, n) {
    console.log(x ** n)
  }
  
  pow(3, 2);
  pow(3, 3);
  pow(1, 100);
  
  const showX = prompt('Enter first number', '');
  const showN = prompt('Enter second number', '');
  
  if (showN < 1) {
    alert(`Power ${showN} is not supported, use a positive integer`);
  } else {
    alert( pow(showX, showN) );
  }
  
  
  // FUNCTION EXPRESSIONS
  function sayHello() {
    console.log('Hello');
  }
  
  sayHello();
  
  // Creating it as an expression
  let sayHello1 = function() {
    console.log( 'Hello friend!' );
  };
  
  // Function as value
  console.log( sayHello );
  
  // Copying a function to another variable
  function sayHello2() {
    console.log( "Hellooo!" );
  }
  
  let func = sayHello2;
  
  func();       // Hellooo!
  sayHello2();   // Hellooo!
  
  /**
   * Repeating the code using 
   * function expression
   */
  
  let sayHello3 = function() {
    console.log("Hello Pals!");
  };
  
  let func1 = sayHello3;
  
  sayHello3();  // Hello Pals!
  func1();      // Hello Pals!
  
  
  // Callback functions
  function ask(question, yes, no) {
  
    if (confirm(question)) yes()
    else no();
  }
  
  function showOk() {
    alert( "You agreed." );
  }
  
  function showCancel() {
    alert( "You cancelled the execution." );
  }
  
  ask("Are you 18 or above?", showOk, showCancel);
  
  // Using function expression
  
  // function ask(question, yes, no) {
  //   if (confirm(question)) yes()
  //   else no();
  // }
  
  // ask(
  //   "Are you above 20yrs of age?",
  //   function() { alert("You agreed."); },
  //   function() { alert("You cancelled the exexution"); }
  // );
  
  
  // FUNCTION EXPRESSION VS FUNCTION DECLARATION
  // Function Declaration
  function sumBoth(a, b) {
    return a + b;
  }
  
  console.log( sumBoth(10, 5) );  // 15
  
  // Function Expression
  let sumBoth1 = function(aa, bb) {
    return aa + bb;
  };
  
  console.log( sumBoth1(20, 50) );  // 70
  
  
  // let age1 = prompt("What is your age?", 18);
  
  // let welcome;
  
  // if (age1 < 18) {
  
  //   welcome = function() {
  //     alert("Hello!");
  //   };
  
  // } else {
  
  //   welcome = function() {
  //     alert("Greetings!")
  //   };
  
  // }
  
  // welcome();
  
  // SIMPLIFYING IT USING A ? OPERATOR
  // let age2 = prompt("How old are you?", 18);
  
  // let welcome1 = (age2 < 18) ?
  //   function () { alert("Hello!!"); } :
  //   function () { alert("Greetings!!"); };
  
  // OR
  
  // let welcome1;
  
  // (age2 < 18) ? welcome1 = function () {
  //   alert("Hello!!!");
  // } : welcome1 = function () {
  //   alert("Greetings!!!");
  // };
  
  // welcome1();
  
  
  // ARROW FUNCTIONS
  let sum1 = (e, f) => e + f;
  
  console.log( sum1(9, 9) );
  console.log( sum1(10, 10) );
  console.log( sum1(11, 11) );
  console.log( sum1(12, 12) );
  
  // One argument - removing parentheses
  let double = g => g * 2;
  
  console.log( double(24) );
  console.log( double(25) );
  console.log( double(26) );
  console.log( double(27) );
  
  // With no argument
  let sayHii = () => console.log( "Hiii!!!" );
  
  sayHii();
  
  let remedy = prompt("What number of solutions have you tried?", "");
  
  let solution = (remedy > 3) ? 
    () => alert("You have worked hard!") :
    () => alert("Please continue.");
  
  solution();
  
  // MULTILINE ARROW FUNCTIONS
  let multi = (x, xx) => { // Curly braces opens a multiline arrow function
    product = x * xx;
    return product;   // Curly braces needs an explicit "return"
  };
  
  console.log( multi(5, 6) );     // 30
  console.log( multi(6, 7) );     // 42
  console.log( multi(7, 8) );     // 56
  
  
  // TASKS
  function ask1(question1, yes1, no1) {
    if (confirm(question1)) yes1();
    else no1();
  }
  
  ask1(
    "Do you love banana?",
    () => alert("You do."),
    () => alert("You do not.")
  );
  
  
  // JAVASCRIPT CALL STACK
  function add(g, gg) {
    return g + gg;
  }
  
  function average(g, gg) {
    return add(g, gg) / 2;
  }
  
  let ggg = average(30, 50);
  console.log(ggg);   // 40
  
  // FUN TASKS
  function add7(num) {
    let add7Plus = num + 7;
    return add7Plus;
  }
  
  console.log( add7(10) );    // 17
  console.log( add7(20) );    // 27
  console.log( add7(30) );    // 37
  
  function multiply(num1, num2) {
    let product = num1 * num2;
    return product; 
  }
  
  console.log( multiply(2, 2) );    // 4
  console.log( multiply(2, 4) );    // 8
  console.log( multiply(2, 6) );    // 12
  
  
  function capitalize(str) {
    let toUpper = str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
    
    return toUpper;
  }
  
  console.log( capitalize("woman") );   // Woman
  console.log( capitalize("MAN") );     // Man
  console.log( capitalize("WONderfuL") );  // Wonderful
  
  
  function lastLetter(str) {
    let lastOfLeter = str.slice(-1);
  
    return lastOfLeter;
  }
  
  console.log( lastLetter("Orange") );   // e
  console.log( lastLetter("Mango") );     // o
  console.log( lastLetter("Banana") );    // a