Zero Parameter Function.
Create one function with zero parameter having a console statement.
function log(){
console.log("Hello World");
};
Simple Function.
Create one function which takes two values as a parameter and print the sum of them as "Sum of 3, 4 is 7".
function sum(a, b){
sum = a+b;
return `Sum of ${a} and ${b} is ${sum}`;
};
Arrowhead Function.
Create one arrow function.
const max = (a, b) => a > b ? a : b;
console.log(max(5, 6));
Print Output.
var x = 21;
var girl = function () {
console.log(x);
var x = 20;
};
girl ();
var x = 21;
var girl = function () {
console.log(x);
var x = 20;
};
girl ();
Undefined
Print Output.
var x = 21;
girl ();
console.log(x)
function girl() {
console.log(x);
var x = 20;
};
var x = 21;
girl ();
console.log(x)
function girl() {
console.log(x);
var x = 20;
};
Undefined
21
Print Output.
var x = 21;
a();
b();
function a() {
x = 20;
console.log(x);
};
function b() {
x = 40;
console.log(x);
};
var x = 21;
a();
b();
function a() {
x = 20;
console.log(x);
};
function b() {
x = 40;
console.log(x);
};
20
40
Factorial Function.
Write a function that accepts parameter n and returns factorial of n.
function factorial(a){
if(a !== 0){
let ans = 1;
for(let i=2; i<=a; i++){
ans = ans * i;
}
return ans;
}
};
console.log(factorial(a));