A Roadmap for Solving JavaScript Interview questions
By: Austin Johnson
Tags: Problem Solving, JavaScript, Programming, Web Development, Coding

A Roadmap for Solving JavaScript Interview questions

A simple template to help you navigate through code challenges and other interview questions

Photo by Fabian Grohs on Unsplash

Introduction

Most coding challenge articles take a question and show you how to solve it. This is great if you already have a good grasp of the language you’re using and knowledge of basic programming concepts. However, starting out can be difficult and setting up a plan to solve a problem can be a large task in itself if you have no prior experience. I wanted to create content that not only shows you how to solve a problem, but also gives you a template to form a plan when you approach a new problem. One of the most important skills a great programmer has is problem solving. The difficult part is there aren’t many resources out there to teach you how to become a better problem solver. To sharpen your problem solving skills you need to be able to make a plan before you write a single line of code. This article will help you breakdown a problem into four distinct parts. The problem description, brain storm, code — sudo code and your actual code, and then a summary of how you solved the problem. This will give you a great technique to solve any coding challenge or interview question and I hope you have some fun doing it! I also wanted to include a better solution than the one I came up with to show there are many different ways to solve one problem.

Problem Description

The number 89 is the first integer with more than one digit that fulfills the property partially introduced in the title of this kata. What's the use of saying "Eureka"? Because this sum gives the same number.

In effect: 89 = 8^1 + 9^2

The next number in having this property is 135 .

See this property again: 135 = 1^1 + 3^2 + 5^3

We need a function to collect these numbers, that may receive two integers a , b that defines the range [a, b] (inclusive) and outputs a list of the sorted numbers in the range that fulfills the property described above.

Let’s see some cases:

sumDigPow(1, 10) == [1, 2, 3, 4, 5, 6, 7, 8, 9]

// 1^1, 2^1 sumDigPow(1, 100) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 89]

If there are no numbers of this kind in the range [a, b] the function should output an empty list.

sumDigPow(90, 100) == []

How I solved it.

First I created a numberArray to hold all my final values I then created a for loop with the initial value set to ‘a’ and looped through the range until ‘i’ was less than or equal to ‘b’. I then created a conditional to see if the number needed to be split into two separate digits. if the digit was greater than or equal to 10 I split the number into individual digits. I then create a new array to hold all my split digits I then looped through the splitDigit array and used Math.pow( ) to set the first number in the array to a power of one, then the second number in the array to a power of two, and so on. I then found the sum of the array using array.reduce and if the sum was equal to i, I pushed it into the number array with the rest of my values If the number was less than 10 I just used Math.pow( ) with a power of 1 to see if the number was equal to i. If the number was equal I pushed it the number array I then return the numberArray.

My Code

**Sudo Code** // get both integers

// a = a, b = b

// (1,10)

// for(var i = a; i < b; ++i) {

// var numberArray = [];

// if integer > 1 split the integer

// 1 > 1 no

// 1 times power of 1

// 1^1 = 1 yes

// push 1 into numberArray

//

// 10 > 1 yes...

// split 10 into two digits

// digitArray = [];

// sumArray = [];

// digitArray = [1,0]

// 1 and 0

// for(var i = 0; i < digitArray.length; ++i) {

// digitArray[i]^i+1

// 1^1 = 1 and 0^2 = 0

// push digits into sum array

// then add the two digits from sum array

// sumArray = [1,0]

// sumArray.reduce ? === 1

// check does 1 === 10 no

// don't push 10

// finally return numberArray

// }

// } **Code** function splitToDigit(n){

return [...n + ''].map(Number)

} function sumDigPow(a, b) {



var numberArray = [];





for(var i = a; i <= b; ++i) {

if(i >= 10) {



var splitDigit = splitToDigit(i);



var newDigit = [];



for(var j = 1; j < splitDigit.length + 1; ++j) {

newDigit.push(Math.pow(splitDigit[j-1], j))

}

if(newDigit.reduce((a, b) => a + b, 0) === i){

numberArray.push(i)

}

}

if(i < 10){

if(Math.pow(i, 1) === i)

numberArray.push(i)

}

}

return numberArray;

} console.log(sumDigPow(1,100));

How I solved it.

First I created a numberArray to hold all my final values I then created a for loop with the initial value set to ‘a’ and looped through the range until ‘i’ was less than or equal to ‘b’. I then created a conditional to see if the number needed to be split into two separate digits. if the digit was greater than or equal to 10 I split the number into individual digits. I then create a new array to hold all my split digits I then looped through the splitDigit array and used Math.pow( ) to set the first number in the array to a power of one, then the second number in the array to a power of two, and so on. I then found the sum of the array using array.reduce and if the sum was equal to i, I pushed it into the number array with the rest of my values If the number was less than 10 I just used Math.pow( ) with a power of 1 to see if the number was equal to i. If the number was equal I pushed it the number array I then return the numberArray.

Best Solution

function sumDigPow(a, b) {

var ans = [];

while(a <= b){

if(a.toString().split('').reduce((x,y,i)=>x + +y ** (i + 1),0) == a)

ans.push(a);

a++;

}

return ans;

}

Thanks for reading! I hope this article gives you a good sense of how to really breakdown a problem. A great plan of attack is key for solving any problem. Feedback is greatly appreciated!

A note from JavaScript In Plain English

We have launched three new publications! Show some love for our new publications by following them: AI in Plain English, UX in Plain English, Python in Plain English — thank you and keep learning!

We are also always interested in helping to promote quality content. If you have an article that you would like to submit to any of our publications, send us an email at submissions@plainenglish.io with your Medium username and we will get you added as a writer. Also let us know which publication/s you want to be added to.