Found 9317 Articles for Object Oriented Programming

Corner digit number difference - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:56:41

93 Views

We are required to write a JavaScript function that takes in a number, constructs a new number from the first and last digit of that number and returns the difference between the original number and the number thus formed.For example: If the input is 34567Then the corner digits number will be −37And the output will be −34530ExampleFollowing is the code −const num = 34567; const cornerDifference = num => {    let temp = Math.abs(num);    let corner = temp % 10;    if(temp < 100){       corner = temp;    }else{       while(temp >= 10){ ... Read More

JavaScript - Constructs a new array whose elements are the difference between consecutive elements of the input array

AmitDiwan
Updated on 15-Sep-2020 11:54:44

110 Views

Suppose, we have an array of numbers like this −const arr = [3, 5, 5, 23, 3, 5, 6, 43, 23, 7];We are required to write a function that takes in one such array and constructs another array whose elements are the difference between consecutive elements of the input array.For this array, the output will be −const output = [-2, 0, -18, 20, -2, -1, -37, 20, 16];ExampleFollowing is the code −const arr = [3, 5, 5, 23, 3, 5, 6, 43, 23, 7]; const consecutiveDifference = arr => {    const res = [];    for(let i = 0; ... Read More

Diagonal product of a matrix - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:52:25

2K+ Views

Suppose, we have a 2-D array representing a square matrix like this −const arr = [    [1, 3, 4, 2],    [4, 5, 3, 5],    [5, 2, 6, 4],    [8, 2, 9, 3] ];We are required to write a function that takes in this array and returns the product of the element present at the principal Diagonal of the matrix.For this array the elements present at the principal diagonal are −1, 5, 6, 3Hence the output should be −90ExampleFollowing is the code −const arr = [    [1, 3, 4, 2],    [4, 5, 3, 5],   ... Read More

Twice repetitive word count in a string - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:50:23

849 Views

We are required to write a JavaScript function that takes in a string that contains some words that are repeated twice, we need to count such words.For example −If the input string is −const str = "car bus jeep car jeep bus motorbike truck";Then the output should be −3ExampleFollowing is the code −const str = "car bus jeep car jeep bus motorbike truck"; const countRepetitive = str => {    const strArr = str.split(" ");    let count = 0;    for(let i = 0; i < strArr.length; i++){       if(i === strArr.lastIndexOf(strArr[i])){          continue; ... Read More

Change string based on a condition - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:47:31

659 Views

We are required to write a JavaScript function that takes in a string. The task of our function is to change the string according to the following condition −If the first letter in the string is a capital letter then we should change the full string to capital letters.Otherwise, we should change the full string to small letters.ExampleFollowing is the code −const str1 = "This is a normal string"; const str2 = "thisIsACamelCasedString"; const changeStringCase = str => {    let newStr = '';    const isUpperCase = str[0].charCodeAt(0) >= 65 && str[0].charCodeAt(0)

JavaScript - Check if array is sorted (irrespective of the order of sorting)

AmitDiwan
Updated on 15-Sep-2020 11:45:15

432 Views

We are required to write a JavaScript function that takes in an array of literals and checks if the array is sorted or not (irrespective of the order of sorting.)Our function should return true if the array is sorted, false otherwise. Following is the code −Exampleconst arr = [1, 3, 56, 87, 99, 102, 144, 255, 456, 788, 999]; const isSorted = arr => {    const { length: l } = arr;    if(l 0 && arr[i-1] < 0;       const con2 = arr[i] < 0 && arr[i-1] > 0;       if(con1 || con2){          return false;       };    };    return true; }; console.log(isSorted(arr)); OutputFollowing is the output in the console −true

Squared sum of n odd numbers - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:43:14

229 Views

We are required to write a JavaScript function that takes in a Number, say n, and finds the sum of the square of first n odd natural Numbers.For example −If the input number is 3. Then the output will be −1^2 + 3^2 + 5^2 = 35Therefore, the output is −35ExampleFollowing is the code −const num = 3; const squaredSum = num => {    let sum = 0;    for(let i = 1; i

Check whether sum of digit of a number is Palindrome - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:41:24

497 Views

We are required to write a JavaScript function that takes in a number, sums its digits and checks whether that sum is a Palindrome number or not. The function should return true if the sum is Palindrome, false otherwise.For example, if the number is 697, then the sum of its digit will be 22, which indeed, is a Palindrome number.Therefore, our function should return true for 697.ExampleFollowing is the code −const num = 697; const sumDigit = (num, sum = 0) => {    if(num){       return sumDigit(Math.floor(num / 10), sum + (num % 10));    };   ... Read More

Swap kth element of array - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:39:36

447 Views

We are required to write a JavaScript function that accepts an array of Numbers and a number, say k (k must be less than or equal to the length of array).And our function should replace the kth element from the beginning with the kth element from the end of the array.ExampleFollowing is the code −const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const swapKth = (arr, k) => {    const { length: l } = arr;    let temp;    const ind = k-1;    temp = arr[ind];    arr[ind] = arr[l-k];    arr[l-k] = temp; }; swapKth(arr, 4); console.log(arr); swapKth(arr, 8); console.log(arr);OutputFollowing is the output in the console −[    0, 1, 2, 6, 4,    5, 3, 7, 8, 9 ] [    0, 1, 7, 6, 4,    5, 3, 2, 8, 9 ]

Split string into groups - JavaScript

AmitDiwan
Updated on 15-Sep-2020 11:37:43

195 Views

Given a string S which consists of alphabets, numbers and special characters.We need to write a program to split the strings in three different strings S1, S2 and S3, such thatThe string S1 will contain all the alphabets present in S, The string S2 will contain all the numbers present in S, andS3 will contain all special characters present in SThe strings S1, S2 and S3 should have characters in the same order as they appear in input.ExampleFollowing is the code −const str = "Th!s String C0nt@1ns d1fferent ch@ract5rs"; const seperateCharacters = str => {    const strArr = str.split(""); ... Read More

Advertisements