Prime Numbers Upto N in JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:37:43

634 Views

Let’s say, we are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers upto n.For example − If the number n is 24, then the output should be −const output = [2, 3, 5, 7, 11, 13, 17, 19, 23];ExampleFollowing is the code −const num = 24; const isPrime = num => {    let count = 2;    while(count < (num / 2)+1){       if(num % count !== 0){          count++;          continue;       };       return false;    };    return true; }; const primeUpto = num => {    if(num < 2){       return [];    };    const res = [2];    for(let i = 3; i

Merging Boolean Array with AND Operator in JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:36:07

772 Views

Let’s say, we have an array of arrays of boolean like this −const arr = [[true, false, false], [false, false, false], [false, false, true]];We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator.Let’s write the code for this function. We will be using Array.prototype.reduce() function to achieve this.ExampleFollowing is the code −const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge = (arr = []) => {    return arr.reduce((acc, val) => {       ... Read More

Find the Difference Between Two Arrays in JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:34:55

554 Views

We have two arrays of numbers like these −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];We are required to write a JavaScript function that takes in two such arrays and returns the element from arrays that are not common to both.ExampleFollowing is the code −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; const difference = (first, second) => {    const res = [];    for(let i = 0; i < first.length; i++){   ... Read More

Counting Below Par Elements from an Array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:31:13

183 Views

We are required to write a function that counts how many of the elements are in the array below / above a given number.Following is our array of Numbers −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78];For example, if the number is 60, the answer should be five elements below it −54, 54, 43, 3, 23and five element par it −65, 73, 78, 76, 78ExampleFollowing is the code −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78]; const belowParNumbers = (arr, num) => {    return arr.reduce((acc, ... Read More

Find the Center of an Array without ES6 Functions in JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:30:04

330 Views

We are required to write an array function midElement() that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops.If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements.ExampleFollowing is the code −const arr = [14, 32, 36, 42, 45, 66, 87]; const array = [13, 92, 83, 74, 55, 46, 74, 82]; const midElement = (arr, ind = 0) => {    if(arr[ind]){       ... Read More

Summing All Unique Values of an Array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:29:05

288 Views

Let’s say, we are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index.For example −If the input array is −const input = [1, 3, 1, 3, 5, 7, 5, 4];Then the output should be −const output = [2, 6, 7, 10, 4];That means all the duplicate ones are summed to index 0 and all the duplicate threes are summed to index 1 and so on.ExampleFollowing is the code −const input = [1, 3, 1, 3, 5, 7, 5, 4]; const mergeDuplicates = arr => ... Read More

Dividing an Array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:27:30

605 Views

Let’s say, we are required to write a function that takes in an array arr of string / number literals as the first argument and a number n as second argument.We are required to return an array of n subarrays, each of which contains at most arr.length / n elements. And the distribution of elements should be like this −The first element goes in the first subarray, second in second, third in third and so on.Once we have one element in each subarray, we again start with filling the first subarray with its second element.Similarly, when all subarrays have two ... Read More

Find Element by Attributes in Python Selenium

Debomita Bhattacharjee
Updated on 18-Sep-2020 12:20:32

4K+ Views

We can find an element by attributes with Selenium webdriver. There are multiple ways to do this. We can use the locators like css and xpath that use attributes and its value to identify an element.For css selector, theexpression to be used is tagname[attribute='value']. There are two types of xpath – absolute and relative. The xpath expression to be used is //tagname[@attribute='value'], the tagname in the expression is optional. If omitted, the xpath expression should be //*[@attribute='value'].Let us consider an element with input tagname. It will be identified with xpath locator with id attribute (//input[@id='txtSearchText']).Examplefrom selenium import webdriver driver = ... Read More

PHP Unsetting References

Malhar Lathkar
Updated on 18-Sep-2020 12:18:59

307 Views

IntroductionIt is possible to break binding between the content and variable by using unset() function. The unset() function doesn't destroy the content but only decouples variable from it.Example Live DemoOutputAfter unsetting, $b can be used as normal vaiablebefore unsetting : 10 10 after unsetting : 10 20Reference can also be removed by assigning variable to NULLExample Live DemoOutputResult of above script is as followsx and y are references 100 x: 200 y:

PHP Spotting References

Malhar Lathkar
Updated on 18-Sep-2020 12:17:24

245 Views

IntroductionMany syntax constructs in PHP are implemented via referencing mechanisms. If reference to a global variable is unset in a function, the same variable in global namespace is not removed.Example Live DemoOutputGlobal $va1 is intact.Hello World, Hello World Hello PHP, Hello PHP Hello PHPdebug_zval_dump() function can be used if a variable has references to other variables

Advertisements