Fat vs Concise Arrow Functions in JavaScript

AmitDiwan
Updated on 17-Jul-2020 11:41:22

237 Views

The consice arrow is more stream lined form of fat arrow functions for one-line functions. If the function body only has one line of code, then there isn’t any need of curly braces {} for function body as conscie arrow functions have implicit return. Also, if there is only one parameter then that can be written without the parenthesis () but if there is no parameter then the parenthesis is necessary.SyntaxFat arrow function −let add = (a, b) =>{return a+b;}Consice arrow function:let add = (a, b)=>a+b;If one parameter only −let add = a=>a+22;Following is the code for fat vs concise ... Read More

Attributes in C++

sudhir sharma
Updated on 17-Jul-2020 11:39:44

4K+ Views

Attributes are modern ways in C++ to standardize things if their code runs on different compilers. Attributes are used to provide some extra information that is used to enforce conditions (constraints), optimization and do specific code generation if required.These are like an information manual for the compilers to do some enforcing which will improve performance of the code. Attributes were first seen in C++ 11 and are important parts of the programming language since then, Also with every version some revisions are continuously made to make them more powerful and better.Let’s see how we can define attributes in C++For different ... Read More

Concise Arrow Functions in JavaScript

AmitDiwan
Updated on 17-Jul-2020 11:39:29

442 Views

The conscise arrow function syntax is as follows −(param1, param2) =>param1+param2It doesn’t have the function keyword and the function body. There is only => between the parameters and function body and if there is single parameter than it can also be written like this −param1=>param1*2It has implicit return if there aren’t curly braces {} after the =>.Following is the code to implement concise arrow functions in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;     ... Read More

Fat Arrow Functions in JavaScript

AmitDiwan
Updated on 17-Jul-2020 11:37:31

329 Views

The fat arrow function syntax looks like the following −(param1, param2, ..)=>{ }It doesn’t have the function keyword and only have the => between the parameters and the function body.Following is the code implementing fat arrow functions in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result{       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Fat arrow functions CLICK HERE Click on the above button to call the add() fat ... Read More

Multiply N Elements with an Associative Operation in C++

sudhir sharma
Updated on 17-Jul-2020 11:35:40

144 Views

In this problem, we are given an integer n which is the number of elements. Our task is to create a program that counts the number of ways to multiply n elements with the associative operation.Associative operations return the same result irrespective of the manner the numbers are arranged.Let’s take an example to understand the problem, Input3Output12Explanation(x*(y*z)), (x*(z*y)), (y*(x*z)), (y*(z*x)), (z*(x*y)), (z*(y*x)), ((x*y)*z), ((y*x)*z), ((x*z)*y), ((z*x)*y), ((z*y)*x), ((y*z)*x).To solve this problem, we will try to find if there is any relation or any type of series that can be created so that we can generalize our results. Let’s see the ... Read More

Passing Parameters to Callback Functions in JavaScript

AmitDiwan
Updated on 17-Jul-2020 11:35:38

438 Views

Following is the code for passing parameters to callback functions in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result{       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Passing parameters to callback functions CLICK HERE Click on the above button to pass parameter to callback function add2 and call it    let resEle = document.querySelector(".result");    function add2(a) {       return a + 2;    }    function multiply9(fn, num) {       return fn(num) * 9;    }    document.querySelector(".Btn").addEventListener("click", () => {       resEle.innerHTML = "The number returned = " + multiply9(add2, 8);    }); OutputOn clicking the ‘CLICK HERE’ button −

Ways to Empty an Array in JavaScript

AmitDiwan
Updated on 17-Jul-2020 11:33:16

202 Views

To empty an array in JavaScript, there are four ways −Setting to new Array − In this we set our array variable to a new empty array.Using length property − In this we set the length property of our array to 0.Using pop − In this we continuously pop the array elements till the length reaches to 0.Using splice − In this we put the start index as 0 and the no of elements to removed as array.length-1.Following is the code to display no. of ways to empty an array in JavaScript −Example Live Demo Document ... Read More

Ways to Paint n Paintings Without Adjacent Colors in C++

sudhir sharma
Updated on 17-Jul-2020 11:32:40

240 Views

In this problem, we are given two integers n and m, where n is the number of paintings and m is the number of colors available. Our task is to create a program that will find the total number of ways in which we can paint the paintings in such a way that no to consecutive paintings have the same color.Let’s take an example to understand the problem, Inputn = 3, m =3Output12ExplanationP1 P2 P3 C1 C2 C3 C1 C3 C2 C1 C2 C1 C1 C3 C1 C2 C1 C2 C2 C3 C2 C2 C1 C3 C2 C3 C1 C3 ... Read More

Implement Asynchronous Loop in JavaScript

AmitDiwan
Updated on 17-Jul-2020 11:31:59

185 Views

Following is the code to implement asynchronous loop in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } Asynchronous loop in JavaScript CLICK HERE Click on the above button to start our asynchronous loop    let resEle = document.querySelector(".result");    async function waitTime(time) {       return new Promise((resolve) => {          setTimeout(resolve, time);       });    }    async function someFunction() {       for (let i = 0; i < 10; i++) {          await waitTime(2000);          resEle.innerHTML += "i = " + i + "";       }    }    document.querySelector(".Btn").addEventListener("click", () => {       someFunction();    }); OutputOn clicking the ‘CLICK HERE’ button the loop will print 1 to 10 −

Ways to Paint Stairs with Two Colors in C++

sudhir sharma
Updated on 17-Jul-2020 11:30:06

195 Views

We are given n stairs and 2 colors (red and yellow) with which these stairs are to be painted. Our task is to count the number of ways in which we can paint the stairs such that no two consecutive steps will be yellow-colored.Let’s take an example to understand the problem, Input3Output5ExplanationThe ways in which stairs can be painted are YRY, RYR, YRR, RRY, RRR. here R denotes red color, Y denotes yellow color.To solve this problem, let’s see the number of ways the stairs can be painted.N = 1, ways(1) = 2 : R, YN = 2, ways(2) = ... Read More

Advertisements