Found 9288 Articles for Object Oriented Programming

How to reduce arrays in JavaScript?

AmitDiwan
Updated on 17-Jul-2020 07:40:50

96 Views

Following is the code to reduce arrays in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,    .sample {       font-size: 18px;       font-weight: 500;       color: red;    }    .result {       color: blueviolet;    }    button {       padding: 8px;    } Reduce arrays javascript [1,3,5,6,9,22,15] Sum Click on the above button to sum the elements of the array    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    let sampleEle = document.querySelector(".sample");    let arr = [1, 3, 5, 6, 9, 22, 15];    sampleEle.innerHTML = arr;    BtnEle.addEventListener("click", () => {       resEle.innerHTML =       "Sum = " +       arr.reduce((sum, prev) => {          return sum + prev;       });    }); OutputOn clicking the ‘Sum’ button −

The generator.throw() method in JavaScript.

AmitDiwan
Updated on 17-Jul-2020 07:42:47

118 Views

The generator.throw() method is used to pass an error to the yield. The generator resumes the execution after throw has been called by throwing an error and returning object with properties done and value.Following is the code for generator.throw() in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result, .sample {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    }    .result {       color: red;    } generator.throw() method ... Read More

Creating ‘Copy to Clipboard’ feature on a web page with JavaScript

AmitDiwan
Updated on 17-Jul-2020 08:42:09

130 Views

Following is the code for creating ‘copy to clipboard’ feature on a web page with JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    }    input,    button {       padding: 8px;    } Creating Copy to Clipboard Copy Text Click on the above button to copy text from the textbox    let resEle = document.querySelector(".result");   ... Read More

Continuing a loop in functional programming JavaScript.

AmitDiwan
Updated on 17-Jul-2020 07:34:13

78 Views

Following is the code for continuing a loop in functional programming JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Continuing a loop in functional programming JavaScript Click here Click on the above button to iterate fruitArr and skip mango and peach or not    let resEle = document.querySelector(".result");    let fruitArr = ["apple", "mango", "peach", "papaya", "watermelon"];    document.querySelector(".Btn").addEventListener("click", () => {       fruitArr.some(function (fruit) {          if (fruit === "mango" || fruit === "peach") {             return false;          }          resEle.innerHTML += fruit + "";       });    }); OutputOn clicking the ‘Click here’ button −

Breaking a loop in functional programming JavaScript.

AmitDiwan
Updated on 17-Jul-2020 07:35:27

103 Views

Following is the code for breaking a loop in functional programming −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Breaking a loop in functional programming JavaScript Click here Click on the above button to iterate fruitArr array and exit if peach is found    let resEle = document.querySelector(".result");    let fruitArr = ["apple", "mango", "peach", "papaya", "watermelon"];    document.querySelector(".Btn").addEventListener("click", () => {       fruitArr.some(function (fruit) {          if (fruit === "peach") {             return true;          }          resEle.innerHTML += fruit + "";       });    }); OutputOn clicking the ‘Click here’ button −

Explain import “as” and Export “as” constructs in JavaScript.

AmitDiwan
Updated on 17-Jul-2020 07:32:33

146 Views

Import as allows us to import a named module under different name.Export as allows us to export a named module under different name.Following is the code for import as and export as construct in Javascript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    } Import as and Export as in JavaScript CLICK HERE Click on the above button to execute the imported ... Read More

How to get all unique values in a JavaScript array?

AmitDiwan
Updated on 17-Jul-2020 07:31:41

354 Views

Following is the code for getting all unique values in a JavaScript array −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result,    .sample {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    }    .result {       color: red;    } Get all unique values in a array Click here Click on the above button to remove the duplicate values from the above array    let resultEle = document.querySelector(".result");    let sampleEle = document.querySelector(".sample");    let arr = [2, 3, 4, 2, 3, 4, "A", "A", "B", "B"];    sampleEle.innerHTML = "arr = " + arr;    document.querySelector(".Btn").addEventListener("click", () => {       let set1 = new Set(arr);       resultEle.innerHTML = "arr = " + [...set1] + "";    }); OutputOn clicking the ‘Click here’ button −

What are Partial functions in JavaScript?

AmitDiwan
Updated on 17-Jul-2020 07:27:35

3K+ Views

Partial functions allow takes a function as an argument and along with it takes arguments of other types too. It then uses some of the arguments passed and returns a function that will take the remaining arguments. The function returned when invoked will call the parent function with the original and its own set of arguments.Following is the code for partial functions in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500; ... Read More

Passing empty parameter to a method in JavaScript

AmitDiwan
Updated on 17-Jul-2020 07:28:12

951 Views

Following is the code passing empty parameter to a method in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: blueviolet;    } Passing empty parameter to a method Click here Click on the above button to call multiply function and pass empty parameters to it    let resEle = document.querySelector(".result");    let BtnEle = document.querySelector(".Btn");    function multiply(a = 2, b = 4) {       return a * b;    }    BtnEle.addEventListener("click", () => {       resEle.innerHTML = "The multiplication of numbers = " + multiply();    }); OutputOn clicking the ‘Click here’ button −

How to borrow methods in JavaScript?

AmitDiwan
Updated on 17-Jul-2020 07:24:59

98 Views

The call(), apply() and bind() are used to borrow methods in JavaScript.Following is the code for borrowing methods in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result, .sample {       font-size: 18px;       font-weight: 500;       color: rebeccapurple;    }    .result {       color: red;    } Borrowing a method in JavaScript CLICK HERE Click on the above button to borrow the welcome method of object obj ... Read More

Advertisements