Skip to content

Instantly share code, notes, and snippets.

@AbudiMutamba
Created June 29, 2021 10:01
Show Gist options
  • Save AbudiMutamba/ea7bc9c0119b9ff9728b4ecbcd9ed51d to your computer and use it in GitHub Desktop.
Save AbudiMutamba/ea7bc9c0119b9ff9728b4ecbcd9ed51d to your computer and use it in GitHub Desktop.
Ablestate cohort1 -JS session 6
/**
* Callbacks: Functions passed as arguments and are involked by the calling function( Function where they are passed)
*
*/
/**
*@Greet
*@param {string} msg - Greeting message
*@param {callback} returnName - Returns name
*@returns {string} - Greeting + name
*/
function HandleName() {
return "David";
}
function Greet(msg, returnName){
return msg + returnName();
}
console.clear();
let greeting = Greet("Good morning ", HandleName);
console.log(greeting);
/**
*Scenario: Payment processing
*Requirements
*Totalcost
*Card Number
*Seller
*Buyer
*/
/**
*@Totalcost
*@returns {number} totalCost - totalCost
*/
let TotalCost = () => 10000;
/**
*@deductedFromCard
*@param {String} cardNumber - The actual valid card number
*@returns {boolean} - If the deduction is true
*/
let deductedFromCard = (cardNumber) => {
if (cardNumber) return true;
return false;
};
/**
*@notifySeller
*@returns {boolean} - true: if notification is sent. false if notififcation is not sent.
*/
let notifySeller = () => true;
/**
*@notifyBuyer
*@returns {boolean} - true: if notification is sent. false if notififcation is not sent.
*/
let notifyBuyer = () => true;
/**
*@processPayment : invoke the function
*@returns : null
*/
console.clear();
function processPayment() {
let totalCost = TotalCost();
if(totalCost){
let result = deductedFromCard("234354353456");
if(result) {
if (notifySeller()){
if (notifyBuyer()) {
console.log("Payment completed");
}
}
} else {"Enter CardNumber"}
}
return null;
}
processPayment();
/**
*Excute after a given time
setTimeout
*setInterval
*/
let count = 1;
function incrementCoutBy10 () {
count += 10;
console.log(count);
}
//setTimeout(incrementCoutBy10, 5000)
const intervalID = setInterval(incrementCoutBy10, 3000);
setTimeout(clearInterval(intervalID), 10000);
/**
*Rcursssion: Factorial
*3! = 1 * 2 * 3
*2! = 1 * 2
*And
* */
let retunFactorial = (input) => {
if (typeof input !== "number") return false;
if(input <= 1) return 1;
return input * retunFactorial(input - 1);
}
let factorialof3 = retunFactorial(3);
console.log ( factorialof3);
/**
* Fibonacci Squence
* Xn+2 = Xn + 1 + Xn
*/
// let generateFibonanci = (input) => {
// // if (typeof input !== "number") return false ;
// if(input <= 1) return 0;
// if(input === 1) return 1;
// let n1 = 0,
// n2 = 1,
// nextTerm;
// n1 = n2;
// n2 = nextTerm;
// nextTerm = n1 + n2;
// // input = (input - 1) + (input - 2)
// return (generateFibonanci(input - 1) + generateFibonanci(input - 2))
// }
// let fibonaciof3 = generateFibonanci(5);
// console.log(fibonaciof(5);
// Asynchronous in java
setTimeout( () => {
console.log('Annoymous function')
},4000);
console.log("Out of function");
// Asynchronous the different processes execute at different time intervals depending on which is faster. So that means processes run independently. But for synchronous, processes run one after the other.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment