ProblemWe are required to write a JavaScript function that lives on the prototype object of the string class.This function should simply change case of all the alphabets present in the string to uppercase and return the new string.ExampleFollowing is the code − Live Democonst str = 'This is a lowercase String'; String.prototype.customToUpperCase = function(){ const legend = 'abcdefghijklmnopqrstuvwxyz'; const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; let res = ''; for(let i = 0; i < this.length; i++){ const el = this[i]; const index = legend.indexOf(el); if(index !== -1){ ... Read More
ProblemSuppose the following number name string −const str = 'TOWNE';If we rearrange this string, we can find two number names in it 2 (TWO) and 1 (ONE).Therefore, we expect an output of 21We are required to write a JavaScript function that takes in one such string and returns the numbers present in the string.ExampleFollowing is the code − Live Democonst str = 'TOWNE'; const findNumber = (str = '') => { function stringPermutations(str) { const res = []; if (str.length == 1) return [str]; if (str.length == 2) return [str, str[1]+str[0]]; ... Read More
ProblemWe are required to write a JavaScript function that takes in an array of numbers (positive and negative). Our function should calculate and return the sum of all the positive numbers present in the array.ExampleFollowing is the code − Live Democonst arr = [5, -5, -3, -5, -7, -8, 1, 9]; const sumPositives = (arr = []) => { const isPositive = num => typeof num === 'number' && num > 0; const res = arr.reduce((acc, val) => { if(isPositive(val)){ acc += val; }; return acc; }, 0); return res; }; console.log(sumPositives(arr));OutputFollowing is the console output −15
ProblemWe are required to write a JavaScript function that takes in an array of integers. Our function should return the sum of all the integers that have an even index, multiplied by the integer at the last index.const arr = [4, 1, 6, 8, 3, 9];Expected output −const output = 117;ExampleFollowing is the code − Live Democonst arr = [4, 1, 6, 8, 3, 9]; const evenLast = (arr = []) => { if (arr.length === 0) { return 0 } else { const sub = arr.filter((_, index) => index%2===0) const sum = sub.reduce((a,b) => a+b) const posEl = arr[arr.length -1] const res = sum*posEl return res } } console.log(evenLast(arr));OutputFollowing is the console output −117
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Demofrom collections import deque def add_edge(adj: list, u, v): adj[u].append(v) adj[v].append(u) def detect_cycle(adj: list, s, V, visited: list): parent = [-1] * ... Read More
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Graph_structure: def __init__(self, V): self.V = V self.adj = [[] for i in range(V)] def DFS_Utility(self, temp, ... Read More
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Demofrom collections import deque def add_edge(v, w): global visited_node, adj adj[v].append(w) adj[w].append(v) def BFS_operation(component_num, src): global visited_node, adj queue ... Read More
When it is required to display the nodes of a tree using the breadth first search traversal, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, perform ‘bfs’ (breadth first search) and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Tree_struct: def __init__(self, data=None): self.key = data self.children = [] def set_root(self, data): self.key = data ... Read More
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Tree_struct: def __init__(self, data=None): self.key = data self.children = [] def set_root(self, data): self.key = ... Read More
ProblemWe are required to write a JavaScript function that takes in a string str. Our function should construct a new string that contains only the unique characters from the input string and remove all occurrences of duplicate characters.ExampleFollowing is the code − Live Democonst str = 'hey there i am using javascript'; const removeAllDuplicates = (str = '') => { let res = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; if(str.indexOf(el) === str.lastIndexOf(el)){ res += el; continue; }; }; return res; }; console.log(removeAllDuplicates(str));OutputFollowing is the console output −Ymungjvcp