DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 968,547 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
King Isaac Nsengiyunva
King Isaac Nsengiyunva

Posted on

Working with JavaScript: Coding Challenge 1

Given a coding challenge; it can be done in any language of your choice. I chose to use Javascript and came up with a nice cool working algorithm.
I wanted to share this with everyone so that we can learn, share, and improve the algorithm.
Challenge: You have an array of random numbers; Create a function that will accept an input and output values for each of the following tasks.

  1. If an input is parsed to the function, check if the the input exists in the array and return that input value.

    [ 5,9, 10, 12, 24 ]
    //if 5 is parsed to the function, return 5

  2. If an input parsed to the function exceeds the largest value in the array, return the current largest value in the array.

    [ 12,53, 59, 250, 150 ]
    //if user parses a value of 500, return the largest value in array which is 250

  3. if an input parsed is not present in the array but lies between any of the array element values, then return the minimum value in the array closest to the parsed input. e.g.
    Given an array as shown below:

    [ 50, 23, 69, 75, 20, 150 , 34 ]
    //if the user parses the value of 70, 70 lies between 69 and 75, and so we return the closest minimum value = 69

  4. If the input parsed to the function is less than the smallest value among all the array element values, then return a message.

    [ 4, 10, 8, 12, 24 ]
    //if the input parsed value = 2, then return a message "value is not available"

These are the main tasks for the algorithm. I have created a sandbox for my JavaScript; you can follow and the link and view the code and console for the results here

However, here is my solution explained; The given array is initialized as follows. I create a function called fnCheckValue


 var arr = [ 2, 8, 10, 12, 148, 24, 150, 74, 6, 45, 34, 850 ];
function fnCheckValue( input ) {

}


Enter fullscreen mode Exit fullscreen mode

First, i check if the input is a number and is not an string/ or any other data type.


  if(parseFloat(input) > 0) {
  //proceed with code
  }

Enter fullscreen mode Exit fullscreen mode

I then sort the array numbers from lowest to largest, then i store the largest value in the array. I also have an empty array for the dynamic values.


 var sortedArray = arr.sort( (a, b) => a - b  );
 var largestValue = sortedArray[sortedArray.length - 1];
 var smallestValue = sortedArray[0];
 let newArray = [];

Enter fullscreen mode Exit fullscreen mode

I then check if the parsed input exists in the array. for this, i use the javaScript function indexOf(arrayElement) explained here
This returns the first index at which the element is within an array. if the element does not exist in the array, the result is -1.
if the value exists in the array, we return that value and stop execution.


  var doesValueExists = sortedArray.indexOf(input);
  if(doesValueExists === -1 ){
   //value does not exist in the array
  } 

Enter fullscreen mode Exit fullscreen mode

At the stage, we have fulfilled one condition. Next we check if the value exceeds the largest value in the array.
Also if the input is less than the smallest value in the array, we return the message accordingly.


   if(doesValueExists === -1 ){
     if(input > largestValue ){
       return largestValue;
     }
     if( input < smallestValue){
       return 'not available';
    }

Enter fullscreen mode Exit fullscreen mode

if the input lies between a range of values between the array elements, we need to check the entire array and obtain the minimum value closest to the input value.


 sortedArray.forEach( (value) => {
  if( value > input ){
   newArray.push(value);
  }
 });

Enter fullscreen mode Exit fullscreen mode

We then sort this new array from ascending to descending. The first element in the array will be the lowest and the minimum value.
We then go and find the index of this value in the original sorted array.


 //sort this new array and obtain the first element
 let minValue = newArray.sort( (a,b) => a - b )[0];

 let minValueIndex = sortedArray.indexOf( minValue );

Enter fullscreen mode Exit fullscreen mode

Now since we know the index of this minimum value, we can go ahead and get its neighbour and return this.


  return sortedArray[minValueIndex-1];

Enter fullscreen mode Exit fullscreen mode

And that's our little cool function that works, to run the application just by invoking the function with a parameter.


 console.log('value is ',fnCheckValue(2));

Enter fullscreen mode Exit fullscreen mode

That's it. Feel free to suggest improvements and workarounds, and maybe even tests. There is something cool i re-learned today, its called time complexity. The amount of time it takes for the algorithm to execute, we can also improve
the time complexity of this little algorithm.

Thanks alot devs.

Lets learn, lets share, lets code.

Top comments (0)

This post blew up on DEV in 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!