Prime Factor Array of a Number in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:30:04

338 Views

We are required to write a JavaScript function that takes in a number and returns an array of all the prime numbers that exactly divide the input number.For example, if the input number is 105.Then the output should be −const output = [3, 5, 7];ExampleThe code for this will be −const num = 105; const isPrime = (n) => {    for(let i = 2; i {    const res = num % 2 === 0 ? [2] : [];    let start = 3;    while(start

Shedding a String Off Whitespace in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:28:32

169 Views

We are required to write a JavaScript function that takes in a string and returns a new string with all the character of the original string just the whitespaces removed.ExampleThe code for this will be −const str = "This is an example string from which all whitespaces will be removed"; const removeWhitespaces = str => {    let newStr = '';    for(let i = 0; i < str.length; i++){       if(str[i] !== " "){          newStr += str[i];       }else{          newStr += '';       };    };    return newStr; }; console.log(removeWhitespaces(str));OutputThe output in the console −Thisisanexamplestringfromwhichallwhitespaceswillberemoved

Sort Array of Literals Using Quick Sort in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:26:19

417 Views

We are required to write a JavaScript function that takes in an array of numbers and uses the quick sort algorithm to sort it.QuickSort:This algorithm is basically a divide and conquer algorithm where we pick a pivot in every pass of loop and put all the elements smaller than pivot to its left and all greater than pivot to its right (if its ascending sort otherwise opposite)ExampleThe code for this will be −const arr = [43, 3, 34, 34, 23, 232, 3434, 4, 23, 2, 54, 6, 54]; // Find a "pivot" element in the array to compare all other ... Read More

Count Special Characters in a String Using JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:24:06

1K+ Views

Let’s say that we have a string that may contain any of the following characters.'!', "," ,"\'" ,";" ,"\"", ".", "-" ,"?"We are required to write a JavaScript function that takes in a string and count the number of appearances of these characters in the string and return that count.ExampleThe code for this will be −const str = "This, is a-sentence;.Is this a sentence?"; const countSpecial = str => {    const punct = "!,\;\.-?";    let count = 0;    for(let i = 0; i < str.length; i++){       if(!punct.includes(str[i])){          continue;       };       count++;    };    return count; }; console.log(countSpecial(str));OutputThe output in the console −5

Check Similarity of Two 2D Arrays in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:22:13

211 Views

We are required to write a JavaScript function that takes in two 2-D arrays and returns a boolean based on the check whether the arrays are equal or not. The equality of these arrays, in our case, is determined by the equality of corresponding elements.Both the arrays should have same number of rows and columns. Also, arr1[i][j] === arr2[i][j] should yield true for all i between [0, number of rows] and j between [0, number of columns]ExampleThe code for this will be −const arr1 = [    [1, 1, 1],    [2, 2, 2],    [3, 3, 3], ]; const ... Read More

Rotate a 2D Matrix in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:19:12

352 Views

Transpose:The transpose of a matrix (2-D array) is simply a flipped version of the original matrix (2-D array). We can transpose a matrix (2-D array) by switching its rows with its columns.ExampleThe code for this will be −const arr = [    [1, 1, 1],    [2, 2, 2],    [3, 3, 3], ]; const transpose = arr => {    for (let i = 0; i < arr.length; i++) {       for (let j = 0; j < i; j++) {          const tmp = arr[i][j];          arr[i][j] = arr[j][i];          arr[j][i] = tmp;       };    } } transpose(arr); console.log(arr);OutputThe output in the console −[ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ]

Filter Out Non-Unique Values in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:17:46

221 Views

We have an array of literals that contains some duplicate values appearing for many times like this −const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4];We are required to write a JavaScript function that takes in this array and pick out all the duplicate entries from the original array and only once.Therefore, for the above array, the output should be −const output = [1, 4, 3, 2];ExampleThe code for this will be −const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; const removeDuplicate = arr => {   ... Read More

Finding Armstrong Numbers in a Given Range in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:15:40

554 Views

A number is called Armstrong number if the following equation holds true for that number: xy...z =xx +yy+...+zz, where n denotes the number of digits in the number.For example:153 is an Armstrong number because −11 +55 +33 = 1 + 125 + 27 =153We are required to write a JavaScript function that takes in two numbers, a range, and returns all the numbers between them that are Armstrong numbers (including them, if they are Armstrong)ExampleThe code for this will be −const isArmstrong = number => {    let num = number;    const len = String(num).split("").length;    let res = ... Read More

Sorting Arrays Using Bubble Sort in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:13:52

379 Views

We are required to write a JavaScript function that takes in an array of literals and sorts it using bubble sort.ExampleThe code for this will be −const arr = [4, 56, 4, 23, 8, 4, 23, 2, 7, 8, 8, 45]; const swap = (items, firstIndex, secondIndex) => {    var temp = items[firstIndex];    items[firstIndex] = items[secondIndex];    items[secondIndex] = temp; }; const bubbleSort = items => {    var len = items.length,    i, j;    for (i=len-1; i >= 0; i--){       for (j=len-i; j >= 0; j--){          if (items[j] < items[j-1]){             swap(items, j, j-1);          }       }    }    return items; }; console.log(bubbleSort(arr));OutputThe output in the console −[    2, 4, 4, 4, 7,    8, 8, 8, 23, 23,    45, 56 ]

Split Number into K-Length Array in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:12:13

249 Views

We are required to write a JavaScript function that takes in two numbers say m and k, and returns an array of size k with all the elements of the resulting array adding up to m.ExampleThe code for this will be −const len = 30; const sum = 121; const splitNumber = (len, sum) => {    const res = [];      for(let i = 0; i < len; i++){       res.push(sum / len);    };    return res; }; console.log(splitNumber(len, sum));OutputThe output in the console −[    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333 ]

Advertisements