function myfunction() {
console.log("function");
};
myfunction() //Should say "function" in the console.
function calculate(x, y, op) {
var answer;
if ( op = "add" ) {
answer = x + y;
};
else if ( op = "sub" ) {
answer = x - y;
};
else if ( op = "multi" ) {
answer = x * y;
};
else if ( op = "divide" ) {
answer = x / y;
};
else {
answer = "Error";
};
return answer;
};
console.log(calculate(15, 3, "divide")); //Should say 5 in console.
//I hope I helped!
// Five examples of non-arrow function expressions
function (x) { return x + x + x }
function (s, n) { return s.length > n }
function (p, n, r, t) { return p * Math.pow(1 + (r / n), n * t) }
function () { return Math.random() * 100 }
function (x, y) {
let xSquared = x * x
let ySquared = y * y
return Math.sqrt(xSquared + ySquared)
}
var x = myFunction(10, 10); // Function is called, return value will end up in x
function myFunction(a, b) {
return a * b; // Function returns the product of a and b
}
function world(params){
//Code to be executed when the function is called.
}
world()
//Explaination
//'world' in the function is the name of the function.
//There are brackets after function name. Those are use to push parameters
//The forth line Calls the function named 'world'
function walkTree(node) {
if (node === null) {
return;
}
// do something with node
for (let i = 0; i < node.childNodes.length; i++) {
walkTree(node.childNodes[i]);
}
}
/*A function statement starts with the function keyword.
It can return a primitive type value, object, or another function.
For example, a function statement can return an object as shown in the following code example:*/
function getProduct(){
let product = {
Id:1,
Title:'Book',
Price: 30
};
return product;
}
let p1 = getProduct();
console.log(p1); // prints product object
//(don't type behind the// type function to after that name it//
function name() {
(name)=name
console.log(name)
};
//{ symbol is used o group together code but you must create n index/array of 2(array3)//
//! Button Click Event
//! regular function
document.querySelector("button").addEventListener('click', handlClick);
function handlClick() {
alert("I got clicked!")//just to show it works
//what to do when click detected
}
//!anonymous function
document.querySelector("button").addEventListener('click',function handlClick() {
alert("I got clicked!")//just to show it works
//what to do when click detected
});