Found 6710 Articles for Javascript

Count total punctuations in a string - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:53:50

647 Views

In the English language, all these characters are considered as punctuations −'!', "," ,"\'" ,";" ,"\"", ".", "-" ,"?"We are required to write a JavaScript function that takes in a string and count the number of appearances of these punctuations in the string and return that count.ExampleLet’s write the code for this function −const str = "This, is a-sentence;.Is this a sentence?"; const countPunctuation = 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(countPunctuation(str));OutputThe output in the console: −5

Equality of two 2-D arrays - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:51:57

375 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 elementsBoth the arrays should have same number of rows and columns −arr1[i][j] === arr2[i][j]The above should yield true for all i between [0, number of rows] and j between [0, number of columns]ExampleLet’s write the code for this function −const arr1 = [    [1, 1, 1],    [2, 2, 2],    [3, 3, 3], ]; const ... Read More

Transpose of a two-dimensional array - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:49:23

6K+ Views

TransposeThe 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.Let’s say the following is our 2d array −const arr = [    [1, 1, 1],    [2, 2, 2],    [3, 3, 3], ];Let’s write the code for this function −ExampleFollowing is the code −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 ] ]

Program to pick out duplicate only once - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:47:20

662 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.So, for the above array, the output should be −const output = [1, 4, 3, 2];ExampleLet’s write the code for this function −const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; const pickDuplicate = arr => { ... Read More

Check Disarium number - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:44:27

395 Views

Disarium Number − All those numbers which satisfy the following equation are dDisarium number −xy...z = x^1 + y^2 + ... + z^nWhere n is the number of digits in the number.For example −175 is a disarium number be: 175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175Let’s write the code for this function −ExampleFollowing is the code −const num = 175; const isDisarium = num => {    const res = String(num)    .split("")    .reduce((acc, val, ind) => {       acc += Math.pow(+val, ind+1);       return acc;    }, 0);    return res === num; }; console.log(isDisarium(num)); console.log(isDisarium(32)); console.log(isDisarium(4334));OutputThe output in the console: −true false false

Converting days into years months and weeks - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:42:20

3K+ Views

We are required to write a JavaScript function that takes in a number (representing the number of days) and returns an object with three properties, namely −weeks, months, years, daysAnd the properties should have proper values of these four properties that can be made from the number of days. We should not consider leap years here and consider all years to have 365 days.For example −If the input is 738, then the output should be −const output = {    years: 2,    months: 0,    weeks: 1,    days: 1 }ExampleLet’s write the code for this function −const days ... Read More

Armstrong numbers between a range - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:33:17

1K+ Views

A number is called Armstrong number if the following equation holds true for that number −xy..z = x^n + y^n+.....+ z^nWhere, n denotes the number of digits in the numberFor example − 370 is an Armstrong number because −3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370We 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).ExampleLet’s write the code for this function −const isArmstrong = number => {    let num = number;   ... Read More

Calculate difference between circumference and area of a circle - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:30:50

196 Views

Circumference of a circle is given by −pi * (r * r)And area of a circle is given by −2 * pi * rWhere r is the radius of the circle.We are required to write a JavaScript function that takes in the radius of circle and calculates the difference between the area and the circumference of the circleExampleLet’s write the code for this function −const rad = 6; const circleDifference = radius => {    const area = Math.PI * (radius * radius);    const circumference = 2 * Math.PI * radius;    const diff = Math.abs(area - circumference);   ... Read More

Program to find largest of three numbers - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:29:09

530 Views

We are required to write a JavaScript function that takes in three or more numbers and returns the largest of those numbers.For example: If the input numbers are −4, 6, 7, 2, 3Then the output should be −7ExampleLet’s write the code for this function −// using spread operator to cater any number of elements const findGreatest = (...nums) => {    let max = -Infinity;    for(let i = 0; i < nums.length; i++){       if(nums[i] > max){          max = nums[i];       };    };    return max; }; console.log(findGreatest(5, 6, 3, 5, 7, 5));OutputThe output in the console −7

Code to implement bubble sort - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:25:25

226 Views

We are required to write a JavaScript function that takes in an array of literals and sorts it using bubble sort. In Bubble Sort, each pair of adjacent elements is compared and the elements are swapped if they are not in order.ExampleLet’s write the code for this function −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;   ... Read More

Advertisements