-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jacob's homework for lesson 5 #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,31 @@ console.log('Problem 1:') | |
|
||
// Add your code below this line | ||
|
||
function blackJack(playerCardScore, dealerCardScore) { | ||
const cards = [playerCardScore, dealerCardScore] | ||
const less21 = cards.filter(cards => { | ||
return cards <= 21 | ||
}) | ||
const max = Math.max(...less21) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jacobkelman cool use of the spread operator combined with |
||
|
||
let result = "" | ||
|
||
if (less21 === undefined || less21.length == 0) { | ||
result = 0 | ||
} else { | ||
result = max | ||
} | ||
|
||
|
||
console.log(result); | ||
|
||
} | ||
|
||
blackJack(19, 21) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jacobkelman nice job, take a look an alternative approach to this problem found here |
||
blackJack(22, 22) | ||
blackJack(19, 22) | ||
blackJack(21, 21) | ||
|
||
// Add your code above this line | ||
|
||
/** added for formatting purposes **/ | ||
|
@@ -91,6 +116,30 @@ console.log('Problem 2:') | |
|
||
// Add your code below this line | ||
|
||
function wordCount(phrase) { | ||
const array = phrase.split(" ").map(x => x.replace(/,/g,'')) | ||
const uniqueValues = [...new Set(array)] | ||
const result = {} | ||
|
||
for (let i = 0; i < uniqueValues.length; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jacobkelman interesting approach, however, you generally want to avoid nesting |
||
let matched = 0 | ||
for(let z = 0; z < array.length; z++){ | ||
if(array[z] == uniqueValues[i]) | ||
matched++; | ||
} | ||
|
||
result[uniqueValues[i]] = matched | ||
|
||
} | ||
|
||
console.log(result) | ||
} | ||
|
||
wordCount("olly olly in come free") | ||
wordCount("Baby shark, doo doo doo doo doo doo") | ||
wordCount("Humpty Dumpty sat on a wall Humpty Dumpty had a great fall") | ||
|
||
|
||
// Add your code above this line | ||
|
||
/** added for formatting purposes **/ | ||
|
@@ -136,6 +185,52 @@ console.log('Problem 3:') | |
|
||
// Add your code below this line | ||
|
||
const scoreTable = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jacobkelman good job using an object to map the letters to their scores be careful with your indentation here |
||
a: 1, | ||
b: 3, | ||
c: 3, | ||
d: 2, | ||
e: 1, | ||
f: 4, | ||
g: 2, | ||
h: 4, | ||
i: 1, | ||
j: 8, | ||
k: 5, | ||
l: 1, | ||
m: 3, | ||
n: 1, | ||
o: 1, | ||
p: 3, | ||
q: 10, | ||
r: 1, | ||
s: 1, | ||
t: 1, | ||
u: 1, | ||
v: 4, | ||
w: 4, | ||
x: 8, | ||
y: 4, | ||
z: 10 | ||
} | ||
|
||
function scrabbleScore(word) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jacobkelman this code is clear and easy to follow, good job |
||
let score = 0 | ||
let letters = word.split('') | ||
|
||
letters.forEach(function(letter) { | ||
score += scoreTable[letter] | ||
} | ||
) | ||
console.log(score) | ||
} | ||
|
||
scrabbleScore("cabbage") | ||
scrabbleScore("javascript") | ||
scrabbleScore("function") | ||
|
||
|
||
|
||
// Add your code above this line | ||
|
||
/** added for formatting purposes **/ | ||
|
@@ -175,6 +270,17 @@ console.log('Problem 4:') | |
|
||
// Add your code below this line | ||
|
||
function isPalindrome(word) { | ||
const reverse = word.split("").reverse().join("") | ||
|
||
console.log(word === reverse); | ||
} | ||
|
||
isPalindrome("noon") | ||
isPalindrome("racecar") | ||
isPalindrome("moon") | ||
isPalindrome("run") | ||
|
||
// Add your code above this line | ||
|
||
/** added for formatting purposes **/ | ||
|
@@ -208,6 +314,22 @@ console.log('Problem 5:') | |
|
||
// Add your code below this line | ||
|
||
function doubleLetters(word) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jacobkelman nice approach |
||
const characters = word.split("") | ||
let count = 0 | ||
|
||
for(let i = 0; i < characters.length; i++){ | ||
if(characters[i] == characters[i+1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jacobkelman be careful with the syntax for the Including the curly braces improves the readability of your code
Also, you should use |
||
count++; | ||
} | ||
|
||
console.log(count > 0) | ||
} | ||
|
||
doubleLetters("loop") | ||
doubleLetters("rune") | ||
doubleLetters("apple") | ||
|
||
// Add your code above this line | ||
|
||
/** added for formatting purposes **/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jacobkelman nice approach used here