Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
mmloveaa / 4-11 diff problems
Created April 11, 2016 16:08
4-11 diff problems
function scoreHand(cards) {
var aces = 0;
var score = cards.reduce(function(sum,card) {
var value = 10;
if(card.match(/\d/)) {
value = parseInt(card);
} else if(card === "A") {
value = 11;
aces++;
}
@mmloveaa
mmloveaa / 4-11 Hacker Rank
Created April 11, 2016 16:06
4-11 Hacker Rank
Imagine you're a stock trader. You'd like to compare how you actually did trading a stock yesterday with how well you could have done. To do this, you'll need to find the maximum profit you could have made by buying once and selling once yesterday.
Given an array of stock prices representing the price of the stock each minute, write a function that returns the best profit you could have made making 1 purchase and 1 sale of that stock.
For example:
var stockPrices = [9, 6, 7, 5, 10, 8];
findMaxProfit(stockPrices)
// returns 5 (buying at $5 and selling for $10)
@mmloveaa
mmloveaa / 4- 6 - Q 28- Highest Product
Created April 7, 2016 08:05
4- 6 - Q28 -Highest Product
function getProduct(arr) {
arr1 = arr.sort(function(a,b){
return b-a
})
// console.log(arr1)
var leftMax = arr1[0] * arr1[1] * arr1[2];
var rightMax = arr1[arr1.length-1] * arr1[arr1.length-2] * arr1[0]
return leftMax > rightMax ? leftMax : rightMax
@mmloveaa
mmloveaa / FCC - slasher
Created April 7, 2016 04:31
FCC - slasher
function slasher(arr, howMany) {
console.log(arr.splice(0,howMany));
console.log(arr)
return arr.slice(howMany);
// it doesn't always pay to be first
// if(howMany > arr.length){
@mmloveaa
mmloveaa / 4-6 Decoder
Last active April 7, 2016 05:59
4-6 Decoder
function decoder(key,code){
return code.map(function(val){
return key[val];
}).join('');
}
decoder("ABCDEFGHIJKLMNOPQRSTUVWXYZ",[2, 14, 3, 8, 13, 6])
@mmloveaa
mmloveaa / 4- 6 Monkey Patching
Last active April 7, 2016 05:55
4-6- Monkey Patching
String.prototype.snakesToCamels = function(){
console.log(this);
var someString = this.toLowerCase().split('_');
return someString.reduce(function(a,b,index){
if(index===0){
return a+b
} else
{
return a+b[0].toUpperCase()+b.slice(1);
@mmloveaa
mmloveaa / Q1-3
Created April 6, 2016 19:15
Q1-3
var x = 100;
function test() {
if(false) {
var x = 199;
}
console.log(x);
}
test();
@mmloveaa
mmloveaa / Q26
Last active April 7, 2016 05:54
Q26
function getProducts(arr){
var totalProd = arr.reduce(function(a,b){return a*b;});
return arr.map(function(val){
return totalProd/val;
});
}
getProducts([5, 1, 3, 2])
@mmloveaa
mmloveaa / 4-6 - Q25 Count String
Last active April 7, 2016 08:04
4-6 - Q25 Count String
Count Strings in Objects
Complete the function stringCount that will count all string values inside an object. The object will be filled with all kinds of data, including arrays and other objects. Your function must find all strings, which may be deeply nested.
Input Format:
The function will have one argument
obj - an object, which may contain all kinds of data.
Output Format:
The function will return a number, representing the number of strings in the object.
@mmloveaa
mmloveaa / 4-6
Last active April 6, 2016 17:03
4-6
Q14
var words = ['one', 'two', 'three'];
var x = words.forEach(function(word) {
return word.toUpperCase();
});
x==> undefined