Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions pset-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Copy link
Contributor

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

return cards <= 21
})
const max = Math.max(...less21)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacobkelman cool use of the spread operator combined with Math.max() to get the highest of the two values


let result = ""

if (less21 === undefined || less21.length == 0) {
result = 0
} else {
result = max
}


console.log(result);

}

blackJack(19, 21)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 **/
Expand Down Expand Up @@ -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++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacobkelman interesting approach, however, you generally want to avoid nesting for loops or if statements if possible - check out an alternative approach here

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 **/
Expand Down Expand Up @@ -136,6 +185,52 @@ console.log('Problem 3:')

// Add your code below this line

const scoreTable = {
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 **/
Expand Down Expand Up @@ -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 **/
Expand Down Expand Up @@ -208,6 +314,22 @@ console.log('Problem 5:')

// Add your code below this line

function doubleLetters(word) {
Copy link
Contributor

@kareemgrant kareemgrant Nov 2, 2019

Choose a reason for hiding this comment

The 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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacobkelman be careful with the syntax for the if statements

Including the curly braces improves the readability of your code

if (condition) {
}

Also, you should use === (strict comparison) instead of ==

count++;
}

console.log(count > 0)
}

doubleLetters("loop")
doubleLetters("rune")
doubleLetters("apple")

// Add your code above this line

/** added for formatting purposes **/
Expand Down