I am new to data structures in JavaScript and am trying to learn Binary Search Trees. I was following along with a blog post and was able to get a working solution to the problem of finding the max depth in a BST, but it’s unclear to me how the recursion is working and how the +1 gets added on
Tag: algorithm
Longest palindrome in a string
I wrote the following function to find the longest palindrome in a string. It works fine but it won’t work for words like “noon” or “redder”. I fiddled around and changed the first line in the for loop from: to and now it works, but I’m not clear on why. My intuition is that if you are checking an odd-length
Recreate an object from an array of objects in Javascript
I could use some help with the following: I’m trying to create a filterable wordpress nav menu using React Tree Menu. The Tree Menu component requires the following data structure: I have set up parent and child pages of the above in wordpress and have also created a hierarchical wordpress menu. I am using the wordpress menu api which returns
Javascript Coin changing / Change making algorithm
So I’ve been trying to create a program in Javascript/jQuery that splits an amount of money into the smallest amount of dollar bills. So far the program only works with one bill, and I’m not too sure how to implement the rest and need a push in the right direction here. If I run this with money = 89; it
How to calculate average time
how to calculate average time with 11:59:00 PM and 12:00:00 AM so that it can be 11:59:30 PM. currently this code is giving me 11:59:30 AM Answer This is a function for if you are looking for an average time given a set of times, regardless of date, given a 24 hr period. It works for 12am – 12pm but
Cartesian product of multiple arrays in JavaScript
How would you implement the Cartesian product of multiple arrays in JavaScript? As an example, should return Answer Here is a functional solution to the problem (without any mutable variable!) using reduce and flatten, provided by underscore.js: Remark: This solution was inspired by http://cwestblog.com/2011/05/02/cartesian-product-of-multiple-arrays/
How to perform a Javascript object recursive search that match my needs?
Good day, I am working on a text-based games written in Javascript. I have variable named map that is an associating object containing another object for each rooms. I have found a little algorithm somewhere and I am not sure how to modify it for my specific task. My variable: I would like the function to return an array of
Tersest way to create an array of integers from 1..20 in JavaScript
What would be the tersest way to create this array: For example, a for loop: Or a while loop: Would there be other examples that would be terser — in other words — less code? I’m thinking of things like in Ruby where the equivalent code I believe would be as simple as 1..20. I’m not aware of syntax like
How might I find the largest number contained in a JavaScript array?
I have a simple JavaScript Array object containing a few numbers. Is there a function that would find the largest number in this array? Answer Resig to the rescue: Warning: since the maximum number of arguments is as low as 65535 on some VMs, use a for loop if you’re not certain the array is that small.
Javascript Array.sort implementation?
Which algorithm does the JavaScript Array#sort() function use? I understand that it can take all manner of arguments and functions to perform different kinds of sorts, I’m simply interested in which algorithm the vanilla sort uses. Answer I’ve just had a look at the WebKit (Chrome, Safari …) source. Depending on the type of array, different sort methods are used: