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 251 of 534
Building 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 − Live Democonst n = 15; const buildSquaresArray = (n = 1, res = []) => { const helper = (res, set, n) => { if(set.size === n){ return true; }; for(let i = 1; i
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 − 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
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 − Live Democonst str = 'hey there'; const num = 21; const ...
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 − Live Democonst num = 24; const buildLexicographically = (num = 1) => { const res = []; const curr = num >= 9 ? 9 : num; for (let i = 1; i
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 − Live Democonst range = [4, 67]; const findSum = ([l, h]) => { let sum = 0; for(let i = l; i
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 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 − Live Democonst str = 'this is simple string'; const convertToNato = (str = '') => { let nato = { a: 'Alfa', b: 'Bravo', c: 'Charlie', d: 'Delta', e: ...
Read MoreConverting 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 − 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
Read MoreFinding whether a number is triangular number in JavaScript
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)
Read MoreFinding quarter based on month index in JavaScript
ProblemWe are required to write a JavaScript function that takes in the 1-based month index and return the quarter, which the month falls in.ExampleFollowing is the code − Live Democonst month = 7; const findQuarter = (month = 1) => { if (month
Read More