Found 10710 Articles for Web Development

Boolean Gates in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:27:09

1K+ Views

ProblemWe are required to write a JavaScript function that takes in an array of Boolean values and a logical operator.Our function should return a Boolean result based on sequentially applying the operator to the values in the array.ExampleFollowing is the code − Live Democonst array = [true, true, false]; const op = 'AND'; function logicalCalc(array, op){    var result = array[0];    for(var i = 1; i < array.length; i++){       if(op == "AND"){          result = result && array[i];       }       if(op == "OR"){          result = result || array[i];       }       if(op == "XOR"){          result = result != array[i];       }    }    return result; } console.log(logicalCalc(array, op));Outputfalse

Displaying likes on a post wherein array specifies the names of people that liked a particular post using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:26:44

224 Views

ProblemWe are required to write a JavaScript function that takes in an array of names (string). This array specifies the names of people that liked a particular post on some social networking site.If the count of likes are less than or equal to three our function should simply return all names saying these people liked the post but if the count is greater than three then our function should return first two names and remaining count.ExampleFollowing is the code − Live Democonst names = ['Ram', 'Manohar', 'Jay', 'Kumar', 'Vishal']; const displayLikes = (names) => {    return [       ... Read More

Reversed array of digits from number using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:39:43

303 Views

Understanding how to reverse an array of digits from a number using JavaScript is a significant skill that empowers developers to manipulate and transform data with precision. In the realm of web development, the ability to reverse the order of digits in a number opens doors to a plethora of possibilities, enhancing algorithms, data processing, and user interactions. This article delves into the intricacies of reversing an array of digits from a number using JavaScript, unraveling the lesser-known techniques and methods that enable developers to efficiently manipulate numerical data. By mastering this technique, developers can unlock the potential to create ... Read More

Preparing numbers from jumbled number names in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:57:55

115 Views

ProblemSuppose the following number name string −const str = 'TOWNE';If we rearrange this string, we can find two number names in it 2 (TWO) and 1 (ONE).Therefore, we expect an output of 21We are required to write a JavaScript function that takes in one such string and returns the numbers present in the string.ExampleFollowing is the code − Live Democonst str = 'TOWNE'; const findNumber = (str = '') => {    function stringPermutations(str) {       const res = [];       if (str.length == 1) return [str];       if (str.length == 2) return [str, str[1]+str[0]]; ... Read More

Removing punctuations from a string using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:36:33

1K+ Views

In the realm of text processing and data manipulation, the removal of punctuations from a string holds significant importance. JavaScript, a versatile programming language, offers developers the tools to accomplish this task with utmost precision and efficiency. While the act of eliminating punctuations may seem mundane, mastering this skill is indispensable when it comes to various text-based applications, such as natural language processing, data analysis, and information retrieval. In this article, we will explore the intricacies of removing punctuations from a string using JavaScript, delving into the lesser-known techniques and functions that enable developers to effectively cleanse textual data. By ... Read More

Sum of all positives present in an array in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:54:20

2K+ Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers (positive and negative). Our function should calculate and return the sum of all the positive numbers present in the array.ExampleFollowing is the code − Live Democonst arr = [5, -5, -3, -5, -7, -8, 1, 9]; const sumPositives = (arr = []) => {    const isPositive = num => typeof num === 'number' && num > 0;    const res = arr.reduce((acc, val) => {       if(isPositive(val)){          acc += val;       };       return acc;    }, 0);    return res; }; console.log(sumPositives(arr));OutputFollowing is the console output −15

Even index sum in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:52:45

512 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers. Our function should return the sum of all the integers that have an even index, multiplied by the integer at the last index.const arr = [4, 1, 6, 8, 3, 9];Expected output −const output = 117;ExampleFollowing is the code − Live Democonst arr = [4, 1, 6, 8, 3, 9]; const evenLast = (arr = []) => {    if (arr.length === 0) {       return 0    } else {       const sub = arr.filter((_, index) => index%2===0)       const sum = sub.reduce((a,b) => a+b)       const posEl = arr[arr.length -1]       const res = sum*posEl       return res    } } console.log(evenLast(arr));OutputFollowing is the console output −117

Filtering string to contain unique characters in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:49:16

614 Views

ProblemWe are required to write a JavaScript function that takes in a string str. Our function should construct a new string that contains only the unique characters from the input string and remove all occurrences of duplicate characters.ExampleFollowing is the code − Live Democonst str = 'hey there i am using javascript'; const removeAllDuplicates = (str = '') => {    let res = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       if(str.indexOf(el) === str.lastIndexOf(el)){          res += el;          continue;       };    };    return res; }; console.log(removeAllDuplicates(str));OutputFollowing is the console output −Ymungjvcp

Finding the greatest and smallest number in a space separated string of numbers using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:25:08

253 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains numbers separated by spaces.Our function should return a string that contains only the greatest and the smallest number separated by space.Inputconst str = '5 57 23 23 7 2 78 6';Outputconst output = '78 2';Because 78 is the greatest and 2 is the smallest.ExampleFollowing is the code − Live Democonst str = '5 57 23 23 7 2 78 6'; const pickGreatestAndSmallest = (str = '') => {    const strArr = str.split(' ');    let creds = strArr.reduce((acc, val) => {    let { greatest, ... Read More

Returning lengthy words from a string using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:24:04

73 Views

ProblemWe are required to write a JavaScript function that takes in a sentence of words and a number. The function should return an array of all words greater than the length specified by the number.Inputconst str = 'this is an example of a basic sentence'; const num = 4;Outputconst output = [ 'example', 'basic', 'sentence' ];Because these are the only three words with length greater than 4.ExampleFollowing is the code − Live Democonst str = 'this is an example of a basic sentence'; const num = 4; const findLengthy = (str = '', num = 1) => {    const strArr ... Read More

Advertisements