Found 6683 Articles for Javascript

Accumulating some value over using a callback function and initial value in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:25:53

157 Views

ProblemWe are required to write a JavaScript function that takes in an array a callback function and an initial value.The function should accumulate a value over the iteration of array and finally return the value just like the Array.prototype.reduce() does.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5]; const sum = (a, b) => a + b; Array.prototype.customReduce = function(callback, initial){    if(!initial){       initial = this[0];    };    let res = initial;    for(let i = initial === this[0] ? 1 : 0; i < this.length; i++){       res = callback(res, this[i]);    };    return res; }; console.log(arr.customReduce(sum, 0));OutputFollowing is the console output −15

Finding nth day of the year in leap and non-leap years in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:24:23

220 Views

ProblemWe are required to write a JavaScript function that takes in a number as the first argument and boolean as the second argument.The boolean specifies a leap year (if it’s true). Based on this information our function should return the date that would fall on the nth day of the year.ExampleFollowing is the code − Live Democonst day = 60; const isLeap = true; const findDate = (day = 1, isLeap = false) => {    if(day > 366){       return undefined;    };    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', ... Read More

Swapping string case using a binary number in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:21:37

132 Views

ProblemWe are required to write a JavaScript function that takes in a string str and a number n. Our function should change the given string str using n.Each bit in n will specify whether or not to swap the case for each alphabetic character in s −If the bit is 1, swap the case; if its 0, leave it as is. When we are finished with the last bit of n, start again with the first bit.And finally, we should return the new string thus formed.ExampleFollowing is the code − Live Democonst str = 'hey there'; const num = 21; const ... Read More

Building a lexicographically increasing sequence of first n natural numbers in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:19:36

64 Views

ProblemWe are required to write a JavaScript function that takes in a number n and return an array containing first n natural number.The only condition is that the numbers should be sorted lexicographically which means all the numbers starting with 1 should come before any starting with 2 or 3 or 4 and so on.ExampleFollowing is the code − Live Democonst num = 24; const buildLexicographically = (num = 1) => {    const res = [];    const curr = num >= 9 ? 9 : num;    for (let i = 1; i

Reversing words present in a string in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:17:40

322 Views

ProblemWe are required to write a JavaScript function that takes in a string that represents a sentence.Our function should reverse the order of words present in the string and return the new string.It means that the last word should become the first, second last should be the second and so on.ExampleFollowing is the code − Live Democonst str = 'this is some random string text'; const reverseWords = (str = '') => {    const strArr = str.split(' ');    strArr.reverse();    const reversedStr = strArr.join(' ');    return reversedStr; }; console.log(reverseWords(str));OutputFollowing is the console output −text string random some is ... Read More

Finding sum of all numbers within a range in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:15:11

599 Views

ProblemWe are required to write a JavaScript function that takes in an array that specifies a range.Our function should find and return the sum of all the natural numbers falling in the range including the range numbers.ExampleFollowing is the code − Live Democonst range = [4, 67]; const findSum = ([l, h]) => {    let sum = 0;    for(let i = l; i

Finding number plate based on registration number in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:14:19

507 Views

ProblemThe car registering system of a city N assigns two types of numbers −The Customer ID − a natural number between 0 and 17558423 inclusively, which is assigned to the car buyers in the following order: the first customer receives ID 0, the second customer receives ID 1, the third customer receives ID 2, and so on;Number Plate − assigned to the car and contains the series ( three Latin lowercase letters from a to z) and the serial number (three digits from 0 to 9).Example − aaa001. Each Number Plate is related to the given Customer ID. For example: ... Read More

Converting a string to NATO phonetic alphabets in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:07:19

625 Views

ProblemWe are required to write a JavaScript function that takes in a string and converts it into NATO phonetic alphabet.The 26 code words are as follows: Alfa, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India, Juliett, Kilo, Lima, Mike, November, Oscar, Papa, Quebec, Romeo, Sierra, Tango, Uniform, Victor, Whiskey, X-ray, Yankee, Zulu.ExampleFollowing is the code − Live Democonst str = 'this is simple string'; const convertToNato = (str = '') => {    let nato = {       a: 'Alfa',       b: 'Bravo',       c: 'Charlie',       d: 'Delta',       e: ... Read More

Converting miles per gallon to kilometer per liter in JavaScript

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

423 Views

ProblemWe are required to write a JavaScript function that takes in a number in miles/gallon and return its equivalent km/litre.ExampleFollowing is the code − Live Democonst num = 25; const converter = (mpg) => {    let LITRES_PER_GALLON = 4.54609188;    let KILOMETERS_PER_MILE = 1.609344;    const ratio = KILOMETERS_PER_MILE / LITRES_PER_GALLON;    return Math.round(100 * mpg * ratio) / 100; }; console.log(converter(num));OutputFollowing is the console output −8.85

Finding whether a number is triangular number in JavaScript

AmitDiwan
Updated on 17-Apr-2021 11:02:23

220 Views

Triangular NumberTriangular number is the number of points that can fill an equilateral triangle.For instance − 9 is a triangular number which makes an equilateral triangle with each side of 4 units.ProblemWe are required to write a JavaScript function that takes in a number and returns true if its a triangular number, false otherwise.ExampleFollowing is the code − Live Democonst num = 9; const isTriangular = (num = 1) => {    let i = 4;    if(num === 1){       return true;    };    if(num === 3){       return true;    };    while(((3 * 1) - 3)

Advertisements