Found 10483 Articles for Web Development

How to group an array of objects by key in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:31:17

1K+ Views

Suppose, we have an array of objects containing data about some cars like this −const arr = [    {       'make': 'audi',       'model': 'r8',       'year': '2012'    }, {       'make': 'audi',       'model': 'rs5',       'year': '2013'    }, {       'make': 'ford',       'model': 'mustang',       'year': '2012'    }, {       'make': 'ford',       'model': 'fusion',       'year': '2015'    }, {       'make': 'kia',       ... Read More

Finding day of week from date (day, month, year) in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:28:11

705 Views

We are required to write a JavaScript function that takes in three argument, namely:day, month and year. Based on these three inputs, our function should find the day of the week on that date.For example: If the inputs are −day = 15, month = 8, year = 1993OutputThen the output should be −const output = 'Sunday'ExampleThe code for this will be −const dayOfTheWeek = (day, month, year) => {    // JS months start at 0    return dayOfTheWeekJS(day, month - 1, year); } function dayOfTheWeekJS(day, month, year) {    const DAYS = [       'Sunday',     ... Read More

Finding the power of a string from a string with repeated letters in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:25:20

532 Views

The power of the string is the maximum length of a non−empty substring that contains only one unique character.We are required to write a JavaScript function that takes in a string and returns its power.For example −const str = "abbcccddddeeeeedcba"Then the output should be 5, because the substring "eeeee" is of length 5 with the character 'e' only.ExampleThe code for this will be −const str = "abbcccddddeeeeedcba" const maxPower = (str = '') => {    let power = 1    const sz = str.length - 1    for(let i = 0; i < sz; ++i) {       ... Read More

Checking for straight lines in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:24:19

483 Views

We are required to write a JavaScript function that takes in an array of arrays. Each subarray will contain exactly two items, representing the x and y coordinates respectively.Our function should check whether or not the coordinates specified by these subarrays form a straight line.For example −[[4, 5], [5, 6]] should return true.The array is guaranteed to contain at least two subarrays.ExampleThe code for this will be −const coordinates = [    [4, 5],    [5, 6] ]; const checkStraightLine = (coordinates = []) => {    if(coordinates.length === 0) return false;    let x1 = coordinates[0][0];    let y1 = coordinates[0][1];    let slope1 = null;    for(let i=1;i

Converting numbers to base-7 representation in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:23:03

736 Views

Like the base−2 representation (binary), where we repeatedly divide the base 10 (decimal) numbers by 2, in the base 7 system we will repeatedly divide the number by 7 to find the binary representation.We are required to write a JavaScript function that takes in any number and finds its base 7 representation.For example −base7(100) = 202ExampleThe code for this will be −const num = 100; const base7 = (num = 0) => {    let sign = num < 0 && '−' || '';    num = num * (sign + 1);    let result = '';    while (num) {       result = num % 7 + result;       num = num / 7 ^ 0;    };    return sign + result || "0"; }; console.log(base7(num));OutputAnd the output in the console will be −202

Add number strings without using conversion library methods in JavaScript

Vivek Verma
Updated on 28-Aug-2025 16:55:34

228 Views

We are required to write a JavaScript program that adds two numbers represented as strings, without using any conversion library or built-in methods. For example, If the input number strings are '11' and '23', the output should be '34'. In JavaScript, a number is considered a string-represented number when the number is enclosed in single quotes ('number') or double quotes ("number"). For example: '123' or "456". If you use the typeof operator on such values, it will return the type as 'string'. Here are a few input and output scenarios that provide a better understanding of the given problem: Scenario ... Read More

Iterate through Object keys and manipulate the key values in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:19:56

261 Views

Suppose, we have an array of objects like this −const arr = [    {       col1: ["a", "b"],       col2: ["c", "d"]    },    {       col1: ["e", "f"],       col2: ["g", "h"]    } ];We are required to write a JavaScript function that takes in one such array and returns the following output.const output = [    {       col1: "b",       col2: "d"    },    {       col1: "f",       col2: "h"    } ];Basically, we want to convert ... Read More

Function to check two strings and return common words in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:17:17

812 Views

We are required to write a JavaScript function that takes in two strings as arguments. The function should then check the two strings for common characters and prepare a new string of those characters.Lastly, the function should return that string.The code for this will be −Exampleconst str1 = "IloveLinux"; const str2 = "weloveNodejs"; const findCommon = (str1 = '', str2 = '') => {    const common = Object.create(null);    let i, j, part;    for (i = 0; i < str1.length - 1; i++) {       for (j = i + 1; j

Filter an array containing objects based on another array containing objects in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:36:43

13K+ Views

Suppose we have two arrays of objects like these −const arr1 = [{id:'1', name:'A'}, {id:'2', name:'B'}, {id:'3', name:'C'}, {id:'4', name:'D'}]; const arr2 = [{id:'1', name:'A', state:'healthy'}, {id:'3', name:'C', state:'healthy'}];We are required to write a JavaScript function that takes in two such arrays. Our function should return a new filtered version of the first array (arr1 in this case) that contains only those objects with a name property that are not contained in the second array (arr2 in this case) with the same name property.Therefore, the output, in this case, should look like −const output = [{id:'2', name:'B'}, {id:'4', name:'D'}];ExampleThe code ... Read More

How to sort an object in ascending order by the value of a key in JavaScript?

Disha Verma
Updated on 11-Mar-2025 16:34:54

797 Views

Sorting an object in ascending order by the value of a key is a common task in JavaScript. Objects are data structures, which are a collection of key-value pairs. Since objects in JavaScript are unordered collections, sorting them directly is not possible. This article will guide you on how to sort an object in ascending order by the value of a key in JavaScript. Understanding the Concept Suppose we have the following object: const obj = { "sub1": 56, "sub2": 67, "sub3": 98, "sub4": 54, ... Read More

Advertisements