Found 6683 Articles for Javascript

Reversing all alphabetic characters in JavaScript

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

82 Views

ProblemWe are required to write a JavaScript function that takes in a string str. The job of our function is to reverse it, omitting all non-alphabetic characters.ExampleFollowing is the code − Live Democonst str = 'exa13mple'; function reverseLetter(str) {    const res = str.split('')    .reverse()    .filter(val => /[a-zA-Z]/.test(val))    .join('');    return res; }; console.log(reverseLetter(str));Outputelpmaxe

Finding nth element of an increasing sequence using JavaScript

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

155 Views

ProblemConsider an increasing sequence which is defined as follows −The number seq(0) = 1 is the first one in seq.For each x in seq, then y = 2 * x + 1 and z = 3 * x + 1 must be in seq too.There are no other numbers in seq.Therefore, the first few terms of this sequence will be −[1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]We are required to write a function that takes in a number n and returns the nth term of this sequence.ExampleFollowing is the code − Live Democonst num = ... Read More

Change the case without using String.prototype.toUpperCase() in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:59:35

493 Views

ProblemWe are required to write a JavaScript function that lives on the prototype object of the string class.This function should simply change case of all the alphabets present in the string to uppercase and return the new string.ExampleFollowing is the code − Live Democonst str = 'This is a lowercase String'; String.prototype.customToUpperCase = function(){    const legend = 'abcdefghijklmnopqrstuvwxyz';    const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';    let res = '';    for(let i = 0; i < this.length; i++){       const el = this[i];       const index = legend.indexOf(el);       if(index !== -1){       ... Read More

Reversing negative and positive numbers in JavaScript

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

737 Views

ProblemWe are required to write a JavaScript function that takes in a number and returns its reversed number.One thing that we should keep in mind is that numbers should preserve their sign; i.e., a negative number should still be negative when reversed.ExampleFollowing is the code − Live Democonst num = -224; function reverseNumber(n) {    let x = Math.abs(n)    let y = 0    while (x > 0) {       y = y * 10 + (x % 10)       x = Math.floor(x / 10)    };    return Math.sign(n) * y }; console.log(reverseNumber(num));Output-422

Boolean Gates in JavaScript

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

998 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

223 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

297 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

Advertisements