This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function decoder(key,code){ | |
return code.map(function(val){ | |
return key[val]; | |
}).join(''); | |
} | |
decoder("ABCDEFGHIJKLMNOPQRSTUVWXYZ",[2, 14, 3, 8, 13, 6]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = 100; | |
function test() { | |
if(false) { | |
var x = 199; | |
} | |
console.log(x); | |
} | |
test(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q14 | |
var words = ['one', 'two', 'three']; | |
var x = words.forEach(function(word) { | |
return word.toUpperCase(); | |
}); | |
x==> undefined | |