Found 9150 Articles for Object Oriented Programming

How to sort an array of integers correctly in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:04:21

377 Views

To sort an array of integers, use sort() in JavaScript.ExampleFollowing is the code −var arrayOfIntegers = [67, 45, 98, 52]; arrayOfIntegers.sort(function (first, second) {    return first - second; }); console.log(arrayOfIntegers);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo310.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo310.js [ 45, 52, 67, 98 ]

WebDriver click() vs JavaScript click().

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:53:23

743 Views

We can click a link with the webdriver click and Javascript click. For the Selenium webdriver click of a link we can use link text and partial link text locator. We can use the methods driver.findElement(By.linkText()) and driver.findElement(By.partialLinkText()) to click.The links in an html code are enclosed in an anchor tag. The link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.linkText()) method. The partial matching link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.partialLinkText()) method. Finally to click on the link the click method is used.Let us see the html ... Read More

Get price value from span tag and append it inside a div after multiplying with a number in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:33:33

1K+ Views

Extract the value and convert it from String to integer to get price value from span tag.ExampleFollowing is the code −            Document           Value=                10                    $(document).ready(function () {       var v = parseInt($(".purchase.amount")       .text()       .trim()       .replace(', ', ''));       var totalValue = v * 10;       console.log(totalValue);   ... Read More

Removing listener from inside outer function in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:28:41

90 Views

To remove listener from outer function, use removeEventListener().ExampleFollowing is the code − Document Press Me    var demoId = document.getElementById('demo');    demoId.addEventListener('click', function fun() {       outerFunction(this, fun);    }, false);    function outerFunction(self, funct) {       console.log('outer function is called....');       self.removeEventListener('click', funct, false);       console.log("Listener has been removed...")    } To run the above program, save the file name anyName.html(index.html) and right click on the file. Select the option “Open with live server” in VS Code editor.OutputThis will ... Read More

How can I merge properties of two JavaScript objects dynamically?

AmitDiwan
Updated on 24-Oct-2020 12:23:58

239 Views

To merger properties of two objects, you can use the concept of {...object1,...object2,... N).ExampleFollowing is the code −var firstObject = {    firstName: 'John',    lastName: 'Smith' }; var secondObject = {    countryName: 'US' }; var mergeObject = {...firstObject, ...secondObject}; console.log(mergeObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo307.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo307.js { firstName: 'John', lastName: 'Smith', countryName: 'US' }

How to get all combinations of some arrays in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:22:27

311 Views

You can use your own function to get all combinations.ExampleFollowing is the code −function combination(values) {    function * combinationRepeat(size, v) {       if (size)          for (var chr of values)       yield * combinationRepeat(size - 1, v + chr);       else yield v;    }    return [...combinationRepeat(values.length, "")]; } var output = combination([4,5]); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo306.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo306.js [ '44', '45', '54', '55' ]

Comparing and filling arrays in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:15:24

135 Views

We are required to write a function that compares two arrays and creates a third array filling that array with all the elements of the second array and filling null for all those elements that are present in the first array but misses out in the second array.For example:If the two arrays are −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h'];Then the output should be −const output = ['f', null, 'h'];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; const compareAndFill ... Read More

Fetching odd appearance number in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:10:45

89 Views

Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times.There will always be only one integer that appears an odd number of times. We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleThe code for this will be −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => { ... Read More

Greatest number in a dynamically typed array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:08:49

142 Views

We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some false values. Our function should return the biggest Number from the array.For example: If the input array is −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];Then the output should be 65.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; const pickBiggest = arr => {    let max = -Infinity;    for(let i = 0; i ... Read More

Finding middlemost element(s) in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:07:14

86 Views

We are required to write a JavaScript function that takes in an array of Numbers. The function should return the middlemost element of the array.For example: If the array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be 4.ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6, 7]; const middle = function(){    const half = this.length >> 1;    const offset = 1 - this.length % 2;    return this.slice(half - offset, half + 1); }; Array.prototype.middle = middle; console.log(arr.middle()); console.log([1, 2, 3, 4, 5, 6].middle());OutputThe output in the console will be −[ 4 ] [ 3, 4 ]

Advertisements