Data Structure
 Networking
 RDBMS
 Operating System
 Java
 MS Excel
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP
- Selected Reading
 - UPSC IAS Exams Notes
 - Developer's Best Practices
 - Questions and Answers
 - Effective Resume Writing
 - HR Interview Questions
 - Computer Glossary
 - Who is Who
 
Javascript Articles - Page 280 of 671
 
			
			256 Views
We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated.For example: If the number is −99Then the output should be −8181because 9^2 is 81 and 1^2 is 1.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 9119; const squared = num => { const numStr = String(num); let res = ''; for(let i = 0; i < numStr.length; i++){ const square = Math.pow(+numStr[i], 2); ... Read More
 
			
			9K+ Views
The given task is to reorder an array in JavaScript. We can reorder the elements in the array by using the following methods. One of the ways to achieve the above task is b using sort() method. The sort() is an in-built method in JavaScript, which sorts the alphabetic elements. By default, it sorts in ascending order. Example Following is the example where the array got reordered in ascending order − const States = ["Telangana", "Uttar Pradesh", "Karnataka", "Kerala", ... Read More
 
			
			353 Views
We are required to write a JavaScript function that recursively sums up the digits of a number until it reduces to a single digit number.We are required to do so without converting the number to String or any other data type.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 546767643; const sumDigit = (num, sum = 0) => { if(num){ return sumDigit(Math.floor(num / 10), sum + (num % 10)); } return sum; }; const sumRepeatedly = num => { while(num > 9){ num = sumDigit(num); }; return num; }; console.log(sumRepeatedly(num));OutputThe output in the console will be −3
 
			
			528 Views
We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [2, 6, 7, 1, 7, 8, 4, 3]; const arr2 = [5, ,7, 2, 2, 1, 3]; const arr3 = [1, 56, 345, 6, 54, 2, 68, 85, 3]; const intersection = (arr1, arr2) => { const res = []; for(let ... Read More
 
			
			122 Views
We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 56563; const digitSum = (num, sum = 0) => { if(num){ return digitSum(Math.floor(num / 10), sum + (num % 10)); } return sum; }; const isPrime = n => { if (n===1){ return false; }else if(n === 2){ ... Read More
 
			
			42K+ Views
The JSON (JavaScript object notation) Object can be created with JavaScript. JSON Object is always surrounded inside the curly brackets {}. The keys must be in strings and values must be in valid JSON data type. The data types like string, number, object, Boolean, array, and Null will be supported by JSON. The keys and values are separated by a colon(":") and a comma separates each key and value pair. Syntax Following is the syntax of JSON Object − var JSONObj = {}; The below example is the basic declaration of a JSON object in JavaScript − var JSONObj ... Read More
 
			
			302 Views
We are required to write a JavaScript function that takes in an array of strings and returns the index of string that is shortest in length.We will simply use a for loop and persist the index of string which is shortest in length.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = ['this', 'can', 'be', 'some', 'random', 'sentence']; const findSmallest = arr => { const creds = arr.reduce((acc, val, index) => { let { ind, len } = acc; if(val.length < len){ ... Read More
 
			
			104 Views
We are required to write a JavaScript function that takes in an array of Numbers and sums all the identical numbers together at one index.For exampleIf the input array is −const arr = [20, 10, 15, 20, 15, 10];Then the output should be −const output = [40, 20, 30];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [20, 10, 15, 20, 15, 10]; const addSimilar = arr => { for(let i = 0; i < arr.length; i++){ while(i !== arr.lastIndexOf(arr[i])){ const ind = arr.lastIndexOf(arr[i]); arr[i] += arr.splice(ind, 1)[0]; }; }; }; addSimilar(arr); console.log(arr);OutputThe output in the console will be −[ 40, 20, 30 ]
 
			
			153 Views
We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays.For exampleIf the two arrays are −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3];Then the output should be −const output = [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; const mergeAlernatively = (arr1, arr2) => { const res = []; for(let i = 0; i < arr1.length + arr2.length; i++){ if(i % 2 === 0){ res.push(arr1[i/2]); }else{ res.push(arr2[(i-1)/2]); }; }; return res; }; console.log(mergeAlernatively(arr1, arr2));OutputThe output in the console will be −[ 4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3 ]
 
			
			48K+ Views
The task we need to accomplish is converting an input string to an array in JavaScript. Whenever we try to break a string word into characters or a sentence into words – splitting into arrays, we have some built-in methods to convert string to an array. In this article, we will discuss how to convert the string to an array using different methods in JavaScript. Using the split() method This method will split the string to an array of substrings. This will return the output in a new array and will not change the existing string. The split() method accepts ... Read More