Implement Custom Function Similar to Array Prototype Includes in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:15:57

1K+ Views

ProblemWe are required to write a JavaScript function that lives on the prototype object of Array.It must take in a literal value, and return true if that value is present in the array it is being called upon, false otherwise.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5, 6, 7, 8]; const num = 6; Array.prototype.customIncludes = function(num){    for(let i = 0; i < this.length; i++){       const el = this[i];       if(num === el){          return true;       };    };    return false; }; console.log(arr.customIncludes(num));Outputtrue

Check If All Array Values Are Smaller Than a Limit Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:15:21

437 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers and a number. We should return true if all the numbers in the array are smaller than the number given as second argument, false otherwise.ExampleFollowing is the code − Live Democonst arr = [5, 34, 23, 14, 78, 45, 78]; const limit = 99; const checkEvery = (arr = [], limit = 1) => {    const allSmaller = arr.every(el => {       return el < limit;    });    return allSmaller; }; console.log(checkEvery(arr, limit));Outputtrue

Check Vowels in Array of Numbers Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:13:00

231 Views

ProblemWe are required to write a JavaScript function that takes in takes in an array of numbers. If in that array there exists any number which is the char code any vowel in ascii we should switch that number to the corresponding vowel and return the new array.ExampleFollowing is the code − Live Democonst arr = [5, 23, 67, 101, 56, 111]; const changeVowel = (arr = []) => {    for (let i=0, l=arr.length; i

Sort String Alphabetically and Insert Underscores using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:12:29

364 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings.Our function should return the first string of the array after sorting it alphabetically and each character of that string should be separated by ‘***’.ExampleFollowing is the code − Live Democonst arr = ['this', 'is', 'some', 'string', 'array']; const specialSort = (arr = '') => {    const copy = arr.slice();    copy.sort();    const el = copy[0];    const res = el    .split('')    .join('***');    return res; }; console.log(specialSort(arr));Outputa***r***r***a***y

Finding Date When Deposited Money Equals Specific Amount in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:12:09

119 Views

ProblemWe have an amount of money amt > 0 and we deposit it with an interest rate of p percent divided by 360 per day on the 1st of January 2021. We want to have an amount total >= a0.Our function should take these three parameters and return the date at which the amount will be equal to the desired amountExampleFollowing is the code − Live Democonst principal = 100; const amount = 150; const interest = 2; const findDate = (principal, amount, interest) => {    const startingDate = new Date('2021-01-01')    const dailyInterestRate = interest / 36000    let ... Read More

Finding Path to the End of Maze Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:11:48

254 Views

ProblemWe are required to write a JavaScript function that takes in a matrix of N * N order. The walls in the matrix are marked by ‘W’ and empty positions are marked by ‘_’We can move in any of the four directions at any point. Our function should return true if we can reach to the end [N - 1, N - 1], false otherwise.ExampleFollowing is the code − Live Democonst maze = [    ['_', 'W', 'W', 'W'],    ['_', 'W', 'W', 'W'],    ['W', '_', '_', 'W'],    ['W', 'W', 'W', '_'] ]; const canFindPath = (m = []) ... Read More

Finding Length, Width, and Height of Sheet Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:10:53

281 Views

ProblemWe are required to write a JavaScript function that takes in the length, height and width of a room.Our function should calculate the number of sheets required to cover the whole room given that the width of one single sheet is .52 units and length is 10 units.For the sake of convenience, we should return the number of rolls such that the length is 15% more than the required length.ExampleFollowing is the code − Live Democonst findSheet = (length, width, height) => {    if(length === 0 || width === 0){       return 0;    }    const roomArea ... Read More

Order Tuples Using External List in Python

AmitDiwan
Updated on 17-Apr-2021 13:07:40

341 Views

When it is required to order the tuples using an external list, the list comprehension, and ‘dict’ method can be used.Below is the demonstration of the same −Example Live Demomy_list = [('Mark', 34), ('Will', 91), ('Rob', 23)] print("The list of tuple is : ") print(my_list) ordered_list = ['Will', 'Mark', 'Rob'] print("The ordered list is :") print(ordered_list) temp = dict(my_list) my_result = [(key, temp[key]) for key in ordered_list] print("The ordered tuple list is : ") print(my_result)OutputThe list of tuple is : [('Mark', 34), ('Will', 91), ('Rob', 23)] The ordered list is : ['Will', 'Mark', 'Rob'] The ordered tuple list is ... Read More

Remove Tuples of Length K in Python

AmitDiwan
Updated on 17-Apr-2021 13:07:20

518 Views

When it is required to remove the tuples of a specific length ‘K’, list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [(32, 51), (22, 13 ), (94, 65, 77), (70, ), (80, 61, 13, 17)] print("The list is : " ) print(my_list) K = 1 print("The value of K is ") print(K) my_result = [ele for ele in my_list if len(ele) != K] print("The filtered list is : ") print(my_result)OutputThe list is : [(32, 51), (22, 13), (94, 65, 77), (70, ), (80, 61, 13, 17)] The value of K is ... Read More

Counting Specific Digits in Squares of Numbers using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:06:36

359 Views

ProblemWe are required to write a JavaScript function that takes in an integer n (n >= 0) and a digit d (0

Advertisements