So I have this data, how can I compute their respective percentages? Thank you Answer A simple reference for you by using reduce()

# Tag: algorithm

## JavaScript – How to calculate the distance from max value in array of objects

I currently have a problems counting the distance between the max value and other values in array. Basically I want to make a new array where the values will be the distances from the max value. What I mean by this: Example I have an array of objects: I want the result of this new algorithm that I am trying

## What does it mean by “second += arr[i][arr.length-i-1]”

I been practicing my algorithm solving skill by solving HackerRank problems. However, one of the problems is giving me a difficult time to understand. It is about calculating absolute difference between sum of diagonals in a square matrix “arr”. If the square matrix is like this: and the solution is like this: So, I came up with this solution: However,

## How to check if any array members can be sum up to the largest of them in JavaScript?

The goal is to create a function that takes an array of numbers as a parameter and checks if the largest of them can be obtained as the sum of any of the other numbers in the array. One condition is that negative numbers can be a part of the array taken as a parameter. The problem The function I

## How to insert an array to another array at each iteration of a for loop in javascript

I have a function to bubble sort and I want to save the array after each swap into another array. The bubble sort is working properly and I can log the array after each swap to the console. But I cant seem to push to the other array properly. Here’s my code : Here’s a screenshot of the console :

## Merge sort time complexity check

Hello guys I was learning about the merge sort so I wrote this function that takes 2 arrays and merges them sorted. Can someone tell me the time complexity of this function? I thought it would be O(n^2) as I am using shift inside a while loop. Is that correct or am I missing something here? Answer The worst case

## Javascript Building a Nested Object (tree) from an very weird array Debugging

I could really use some help Debugging a problem. Im trying to build a nested object (tree) from a very unusual array im getting from a backend (dont have access to the BE so i have to work with what i got). I’ve gotten 95 percent of it done but im running into a bug I know there are many

## Finding a string of length 6 in a 6^26 array of strings [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 19 days ago. Improve this question I have a task to create a JS script that is able to find a string using binary search on

## Return sequence with elements from array A except those that are present in B p times

I want to write a function that receives two sequences: A and B and returns sequence C which should contain all elements from A (in order) except those that are present in B p times. For example sequences A=[2,3,9,2,5,1,3,7,10] B=[2,1,3,4,3,10,6,6,1,7,10,10,10] Should return C=[2,9,2,5,7,10] When p = 2 I wrote it like this: But is there a better way to make

## The elegant way to resolve negative zero in Javascript

I have to multiply the sign of all elements in an array. For example 1: Ex2: Ex3: And here is my solution However, the output of ex3 cal([1, -2, 3, 0]) is -0. I’ve already thought about adding one more condition like this And obviously, It looks ugly. Is there a more elegant way to resolve that? Answer In order