<file_sep># javascript-problems
Simple toy problems for fun and profit in JavaScript.

Actually, there is no profit.

This repo is a collection of simple to interesting challenges (usually algorithmic) that I've come across.
They are solved in JavaScript with an attempt at functional or dynamic programming concepts.

Please feel free to use or study any of the material. I don't claim to be an expert by any means, but if any of this helps you it'd be awesome to know!

If you find an issue, feel free to make a pull request!
<file_sep>// NOTES: Standard Function Definition

// REQUIRE: lodash/underscore
// FUNCTION: Find all the peaks in a given array.
// INPUT: Array
// RETURN: index, peak/value within array
//   EX: [2,5]
// ERROR: false

// TIME_COMPLEXITY: O(n)
var peakFinder = function(array) {
  // instantiate an array as result
  var result = [];
  // iterate over input
  _.each(array, function(val,key,col){
    // check left and right neighbors
    if(col[key+1] < val && col[key-1] < val) {
      // add information to results array
      result.push([key,val]);
    }
  });
  // ternary check: if results array is not empty give result array, else give false
  return result.length ? result : false;
};

// NOTES: Prototypical Inheritance

// REQUIRE: lodash/underscore
// FUNCTION: Find all the peaks in a given array.
// INPUT: context/this
// RETURN: index, peak/value within array
//   EX: [2,5]
// ERROR: false

// TIME_COMPLEXITY: O(n)
Array.prototype.findPeaks = function() {
  // instantiate an array as result
  var result = [];
  // iterate over input
  _.each(this, function(val,key,col){
    // check left and right neighbors
    if(col[key+1] < val && col[key-1] < val) {
      // add information to results array
      result.push([key,val]);
    }
  });
  // ternary check: if results array is not empty give result array, else give false
  return result.length ? result : false;
};