Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Javascript Articles
Page 15 of 534
Converting miles per gallon to kilometer per liter in JavaScript
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 −const 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
Read MoreConverting a string to NATO phonetic alphabets in JavaScript
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 −const str = 'this is simple string'; const convertToNato = (str = '') => { let nato = { a: 'Alfa', b: 'Bravo', c: 'Charlie', d: 'Delta', e: 'Echo', ...
Read MoreFinding number plate based on registration number in JavaScript
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 MoreFinding sum of all numbers within a range in JavaScript
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 −const range = [4, 67]; const findSum = ([l, h]) => { let sum = 0; for(let i = l; i
Read MoreBuilding a lexicographically increasing sequence of first n natural numbers in JavaScript
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 −const num = 24; const buildLexicographically = (num = 1) => { const res = []; const curr = num >= 9 ? 9 : num; for (let i = 1; i
Read MoreSwapping string case using a binary number in JavaScript
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 −const str = 'hey there'; const num = 21; const swapCase ...
Read MoreAccumulating some value over using a callback function and initial value in JavaScript
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 −const 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
Read MoreBuilding an array of specific size with consecutive element sum being perfect square in JavaScript
We are required to write a JavaScript function that takes in a number n. Our function should return an array of integers 1..n arranged in a way, such that the sum of each 2 consecutive numbers is a square.ExampleThe code for this will be −const n = 15; const buildSquaresArray = (n = 1, res = []) => { const helper = (res, set, n) => { if(set.size === n){ return true; }; for(let i = 1; i
Read MoreReturning the expanded form of a number in JavaScript
ProblemWe are required to write a JavaScript function that takes in a number and returns a string of the expanded form of the number, indicating the place value of each number.ExampleFollowing is the code −const num = 56577; const expandedForm = (num = 0) => { const str = String(num); let res = ''; let multiplier = Math.pow(10, str.length - 1); for(let i = 0; i < str.length; i++){ const el = +str[i]; const next = +str[i + 1]; if(el){ res += (el * multiplier); }; if(next){ res += ' + '; }; multiplier /= 10; }; return res; }; console.log(expandedForm(num));OutputFollowing is the console output −50000 + 6000 + 500 + 70 + 7
Read MoreCurrified function that multiples array elements in JavaScript
ProblemWe are required to write a JavaScript function that takes in an array and returns another function which in turn takes in a number which returns a new array which is the product of corresponding elements of the input array to the first function and the number provided to the second function.ExampleFollowing is the code −const arr = [2, 5, 2, 7, 8, 4]; const num = 4; const produceWith = (arr = []) => (num) => { const res = arr.map(el => { return el * num; }); return res; }; console.log(produceWith(arr)(num));OutputFollowing is the console output −[ 8, 20, 8, 28, 32, 16 ]
Read More