Found 6710 Articles for Javascript

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

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

324 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, 8, 9];Then the output should be −const newArrayOne = [1, 5, 9, 13, 17]Let's write the code for this function −Exampleconst arrayOne = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const doubleSum = arr => {    const res = [];    for(let i = 0; i < arr.length; i += 2){       res.push(arr[i] + (arr[i+1] || 0));    };    return res; }; console.log(doubleSum(arrayOne));OutputThe output in the console will be −[ 1, 5, 9, 13, 17 ]

How to check whether multiple values exist within a JavaScript array

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 the code and check for multiple values −Exampleconst arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const contains = (first, second) => {    const indexArray = first.map(el => {       return second.indexOf(el);    });    return indexArray.indexOf(-1) === -1; } console.log(contains(arr1, arr2));OutputThe output in the console will be −true

Loop backward in array of objects JavaScript

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

155 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: "Frank", RowNumber: 3.2 },    {id:7, Name: "Gabe", RowNumber: 4 },    {id:8, Name: "Helen", RowNumber: 5 },    {id:9, Name: "Isabelle", RowNumber: 6 },    {id:10, Name: "Jane", RowNumber: 7 },    {id:11, Name: "Ken", RowNumber: 8 }, ];We are required to write a JavaScript function that takes in ... Read More

Search and update array based on key JavaScript

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 arr1 that have the same value for property "POSITION"Let's write the code for this function −Examplelet arr1 = [{"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSI TION":"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"} ] const formatArray = (first, second) => {    second.forEach((el, ... Read More

How to split last n digits of each value in the array with JavaScript?

AmitDiwan
Updated on 31-Aug-2020 13:39:47

312 Views

We have an array of literals like this −const arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225];We are required to write a JavaScript function that takes in this array and a number n and if the corresponding element contains more than or equal to n characters, then the new element should contain only the last n characters otherwise the element should be left as it is.Let's write the code for this function −Exampleconst arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225]; const splitElement = (arr, num) => {    return arr.map(el => {       if(String(el).length

JavaScript Return an array that contains all the strings appearing in all the subarrays

AmitDiwan
Updated on 31-Aug-2020 13:37:24

296 Views

We have an array of arrays like this −const arr = [    ['foo', 'bar', 'hey', 'oi'],    ['foo', 'bar', 'hey'],    ['foo', 'bar', 'anything'],    ['bar', 'anything'] ]We are required to write a JavaScript function that takes in such array and returns an array that contains all the strings which appears in all the subarrays.Let's write the code for this functionExampleconst arr = [    ['foo', 'bar', 'hey', 'oi'],    ['foo', 'bar', 'hey'],    ['foo', 'bar', 'anything'],    ['bar', 'anything'] ] const commonArray = arr => {    return arr.reduce((acc, val, index) => {       return acc.filter(el => val.indexOf(el) !== -1);    }); }; console.log(commonArray(arr));OutputThe output in the console will be −['bar']

Reverse numbers in function without using reverse() method in JavaScript

AmitDiwan
Updated on 31-Aug-2020 13:32:48

1K+ Views

We are required to write a JavaScript function that takes in a number and returns its reversed number with converting it to an array or string.Let's write the code for this function −Exampleconst num = 234567; const reverseNumber = (num, res = 0) => {    if(num){       return reverseNumber(Math.floor(num / 10), (res*10)+(num % 10));    };    return res; }; console.log(reverseNumber(num)); console.log(reverseNumber(53536)); console.log(reverseNumber(76576));OutputThe output in the console will be −765432 63535 67567

Maximum products of two integers in linear time in JavaScript

AmitDiwan
Updated on 31-Aug-2020 13:29:48

139 Views

We are required to write a JavaScript function that takes in an array of Numbers with positive as well as negative numbers and returns the maximum products of two numbers in one traversal.Let's write the code for this function −Exampleconst arr = [-1, -3, -4, 2, 0, -5]; const arr2 = [2, 3, 5, 7, -7, 5, 8, -5]; const produce = arr => arr.reduce((acc, val) => acc*val); const maximumProduct = (arr = []) => {    const [first] = arr;    if(!first){       return 0;    };    const creds = arr.reduce((acc, val) => {     ... Read More

Separate odd and even in JavaScript

AmitDiwan
Updated on 31-Aug-2020 13:02:05

748 Views

We are required to write a JavaScript function that takes in an array of numbers and returns an array with all even numbers appearing on the left side of any odd number and all the odd numbers appearing on the right side of any even number.Therefore, let's write the code for this function −Exampleconst arr = [2, 6, 3, 7, 8, 3, 5, 4, 3, 6, 87, 23, 2, 23, 67, 4]; const isEven = num => num % 2 === 0; const sorter = (a, b) => {    if(isEven(a) && !isEven(b)){       return -1;    };    if(!isEven(a) && isEven(b)){       return 1;    };    return 0; }; arr.sort(sorter); console.log(arr);OutputThe output in the console will be −[    2, 6, 8, 4, 6, 2,    4, 3, 7, 3, 5, 3,    87, 23, 23, 67 ]

Wildcard matching of string JavaScript

AmitDiwan
Updated on 31-Aug-2020 12:51:07

2K+ Views

We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function should return true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, otherwise the function should return false.Let's write the code for this function −Exampleconst str1 = 'first string'; const str2 = 'second string'; const wildcardMatching = (first, second, num) => {    let count = 0;    for(let i = 0; i ... Read More

Advertisements