Sitemap
Blossom

Blossom

learn, unlearn, re-learn. </>

Solving Algorithms in Javascript

4 min readAug 22, 2020

--

Press enter or click to view image in full size
algorithms with javascript image
algorithms with javascript

In this article, I would be solving two algorithms in javascript. The first one is to reduce an object to a single value, and the second one is Get the nearest largest number at index i. These questions are gotten from ‘Daily coding problem’ https://www.dailycodingproblem.com/. It is a mailing list for coding interview problems and I think it is a highly important tool for every software developer to take advantage of, as solving algorithmic problems is at the core of programming, and I could write a whole separate article on the importance of writing algorithms.

This article will, however, be focused on solving those two algorithms in javascript. I will be working with the assumption that if you are here, you are familiar with algorithms and javascript programming language.

For in-depth analysis on the topic of algorithms and data structures in javascript, do check out this highly detailed articles: https://medium.com/siliconwat/algorithms-in-javascript-b0bed68f4038 https://medium.com/siliconwat/data-structures-in-javascript-1b9aed0ea17c

Now, let’s solve some problems.

Reduce an object to a single value

For a given object of strings; const voteCuml = {one: ‘1’, two: ‘7’, three: ‘10’, four: ‘40’}. We are required to return a single value, i.e the total sum (or difference, division etc) of the values, which are strings.

Solution

First, I like to highlight the problem and the steps I would likely take

  1. Get all the value pairs in the given object
  2. Convert it from an array of strings to an array of Numbers
  3. Perform a mathematical operation on it. In this instance, I just want to sum them. (You can do anything).

Get all the value pairs in the given object: There are a lot of ways to go about this, but I have chosen to use the object.values() method. If you a following along, I would suggest opening your code development area to test this out.

let flattenObj = object.values(voteCuml)
This will return an array of the values in the object [‘1’, ‘7’, ‘10’, ‘40]

Next, Convert it from an array of string to an array of numbers: Next, we will be converting the array of strings to an array of numbers. This is so we can perform mathematical operations on it.I have chosen to this using the es6 map method.

let convertToNumbers = flattenObj.map(Number)

map() works on each element in an array and the ‘Number’ function callback is another javascript method that converts a string to an integer. Map returns an array so we will be left with [1, 7, 10, 40]

Perform a mathematical operation on it. In this instance, I just want to sum them. (You can do anything). Sum up the numbers in an array to return a single value. Once again, I will be using an es6 method called reduce. The reduce method performs a function(that you specify) on each element in an array to give back a single value.
More about reduce method here: https://dev.to/blossom/understanding-the-basics-of-javascript-reduce-method-54kn

Get Blossom’s stories in your inbox

Join Medium for free to get updates from this writer.

let returnASingleVal = convertToNumbers.reduce((acc, currVal) => acc + currVal)
This should give us the sum of all the numbers, 58

on to our second javascript problem.

Get the nearest largest number at index i

Given an array of numbers and index i, return the index of the nearest larger number of the number at index i, where distance is measured in array indices.

For example, given [4, 1, 3, 5, 6] and index 0, you should return 3.
5 is the nearest largest number to the element at index 0 (4), and the index of 5 is 3.

Solution

Again, highlight the problem and the steps I would likely take

  1. Create a function that takes two parameters; an array and an index.
  2. Get the element at the specified index i
  3. Get the index of nearest largest number to that element

Create a function that takes two parameters; an array and an index: You can choose to use a function expression, arrow function but I will be using plain old functions.

function arrIndex (arr, index){
//block of code
}
arrIndex([[4, 1, 3, 5, 6], 0)

Get the element at the specified index i: The next thing to do is get the element that is at the index provided, which in this case is index 0.

We can easily get that by using the index on the array
const el = arr[index]
This would return the element at that index //4

Get the index of the nearest largest number to that element: I have chosen to use the findIndex method (an es6 method) to get the index of the nearest largest number from 4

const LargestNum = arr.findIndex(item => {return item > el})
The nearest largest number from 4 is 5, and it’s index 3 will be returned //3

In the codepen playground, I have added an if statement to return null if the number picked has no nearest values.

We have been able to solve two algorithms in javascript, but there are more to play around with. If anything wasn’t clear, write a comment. Try out different methods to solve this problem. Happy coding!

--

--

Written by Blossom

learn, unlearn, re-learn. </>