AmitDiwan has Published 10744 Articles

addEventListener() not working more than once with a button in JavaScript?

AmitDiwan

AmitDiwan

Updated on 01-Sep-2020 11:05:13

2K+ Views

To make addEventListener() repeatedly work on button clicks, use the following code −Example Live Demo Document Click Me    var eventValue = function (event) {       document.body.appendChild(document.createElement('div'))       .textContent = event.type;    }    var pressed ... Read More

JavaScript Remove all '+' from array wherein every element is preceded by a + sign

AmitDiwan

AmitDiwan

Updated on 01-Sep-2020 11:02:25

163 Views

Let’s say the following is our array with elements preceded by a + sign −var studentNames = [    '+John Smith',    '+David Miller',    '+Carol Taylor',    '+John Doe',    '+Adam Smith' ];To remove the + sign, the code is as follows −ExamplestudentNames = [    '+John Smith',   ... Read More

JavaScript Sum function on the click of a button

AmitDiwan

AmitDiwan

Updated on 01-Sep-2020 10:40:22

4K+ Views

Let’s say the following is our button −Sum We are calling the function addTheValue(10) with parameter 10 on button click. On clicking the button, we are adding the value 10 as in the below code −Example Live Demo Document ... Read More

JavaScript get the length of select options(dropdown)?

AmitDiwan

AmitDiwan

Updated on 01-Sep-2020 10:36:50

1K+ Views

Let’s say the following is our dropdown (select) −    John    Mike    Sam    David    Carol Following is the code to get the length of list options in JavaScript −Example Live Demo Document John Mike ... Read More

Access previously iterated element within array.map in JavaScript?

AmitDiwan

AmitDiwan

Updated on 31-Aug-2020 13:52:35

298 Views

Let’s say the following is our array −var details = [    {subjectId:110, subjectName: 'Java' },    {subjectId:111, subjectName: 'Javascript' },    {subjectId:112, subjectName: 'MySQL' },    {subjectId:113, subjectName: 'MongoDB' } ];Now, use the concept of map(). The code is as follows −Examplevar details = [    {subjectId:110, subjectName: 'Java' ... Read More

How to count digits of given number? JavaScript

AmitDiwan

AmitDiwan

Updated on 31-Aug-2020 13:50:58

3K+ Views

The requirements here are simple, we are required to write a JavaScript function that takes in a number and returns the number of digits in it.For example −The number of digits in 4567 is 4 The number of digits in 423467 is 6 The number of digits in 457 is ... Read More

Add two consecutive elements from the original array and display the result in a new array with JavaScript

AmitDiwan

AmitDiwan

Updated on 31-Aug-2020 13:50:00

323 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array.For example, if the input array is −const arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, ... Read More

How to check whether multiple values exist within a JavaScript array

AmitDiwan

AmitDiwan

Updated on 31-Aug-2020 13:47:44

1K+ Views

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not.Following are our arrays −const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89];Let's write ... Read More

Loop backward in array of objects JavaScript

AmitDiwan

AmitDiwan

Updated on 31-Aug-2020 13:46:30

154 Views

We have an array of objects like this −let data = [    {id:1, Name: "Abe", RowNumber: 1 },    {id:2, Name: "Bob", RowNumber: 2 },    {id:3, Name: "Clair", RowNumber: 3 },    {id:4, Name: "Don", RowNumber: 3.0 },    {id:5, Name: "Edna", RowNumber: 3.1 },    {id:6, Name: ... Read More

Search and update array based on key JavaScript

AmitDiwan

AmitDiwan

Updated on 31-Aug-2020 13:41:43

200 Views

We have two arrays like these −let arr1 = [{"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSITION":"GMH"}] let arr2 = [{"EMAIL":"test1@stc.com", "POSITION":"GM"}, {"EMAIL":"test2@stc.com", "POSITION":"GMH"}, {"EMAIL":"test3@stc.com", "POSITION":"RGM"}, {"EMAIL":"test3@CSR.COM.AU", "POSITION":"GM"} ]We are required to write a function that adds the property level to each object of arr2, picking it up from the object from ... Read More

Advertisements