From 42df3d9da4d693935ea8adeeb1e3eb3c52570976 Mon Sep 17 00:00:00 2001 From: Sade Date: Sat, 28 Sep 2019 15:54:34 -0700 Subject: [PATCH] completed hw --- pset-functions.js | 138 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 2 deletions(-) diff --git a/pset-functions.js b/pset-functions.js index 8ef5a24..c363222 100644 --- a/pset-functions.js +++ b/pset-functions.js @@ -30,7 +30,34 @@ Use the following test cases to confirm your program meets the success criteria console.log('Problem 1:') // Add your code below this line - +function blackJack (playerCardScore, dealerCardScore) { + // first, let's determine if there are any entries above 21 + if ((playerCardScore > 21) && (dealerCardScore > 21)) { + return 0 + } else if (playerCardScore > 21) { + return dealerCardScore + } else if (dealerCardScore > 21) { + return playerCardScore + } else { + // if none of the entries are above 21 the score will be calculated + calculateScore(playerCardScore, dealerCardScore) + } +} + +function calculateScore (player, dealer) { + // calculate the difference from 21 for each score + const playerScore = 21 - player + const dealerScore = 21 - dealer + + // if playerScore has a larger difference from 21 then return dealerScore + if (playerScore > dealerScore) { + return dealerScore + } else { + return playerScore + } +} + +console.log(blackJack(19, 22)) // Add your code above this line /** added for formatting purposes **/ @@ -91,6 +118,26 @@ console.log('Problem 2:') // Add your code below this line +function wordCount (phrase) { + const words = phrase.split(' ') + // empty array for counting words + const wordCounter = {} + + // for each word in the phrase + words.forEach((word) => { + if (wordCounter[word] === undefined) { + // debug + // console.log ('word doesnt exist. adding word') + wordCounter[word] = 1 + } else { + wordCounter[word] = wordCounter[word] + 1 + } + }) + + return wordCounter +} + +console.log(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 +183,53 @@ console.log('Problem 3:') // Add your code below this line +const pointChart = [ + { letters: 'aeioulnrst', points: 1 }, + { letters: 'dg', points: 2 }, + { letters: 'bcmp', points: 3 }, + { letters: 'fhvwy', points: 4 }, + { letters: 'k', points: 5 }, + { letters: 'jx', points: 8 }, + { letters: 'qz', points: 10 } +] + +function scrabbleScore (word) { + const lettersInWord = word.split('') + let wordScore = 0 + + // iterate over the letters in the submitted word + lettersInWord.forEach(function (letter) { + // interate over the points array and find which letter + // is currently selected + pointChart.forEach((value) => { + // debug + // console.log(value['letters']) + + // create an array of letters that align the current + // amount of points to be given. these letters will be compared + // against the current letter in the word that was submitted + const comparisonLetters = value.letters.split('') + + // debug + // console.log(comparisonLetters) + + // find if the current letter from the submitted word exists + // within the set of letters for this point value. + // if it does, add points. if not, the set the loop proceeds + // to the next set of letters associated with value['points'] + if (comparisonLetters.find(element => element === letter)) { + wordScore += (value.points) + + // debug + // console.log(`new wordScore is ${wordScore}`) + } + }) + }) + + return (`This word is worth ${wordScore} points`) +} + +console.log(scrabbleScore('function')) // Add your code above this line /** added for formatting purposes **/ @@ -174,7 +268,23 @@ Use the following test cases to confirm your program meets the success criteria console.log('Problem 4:') // Add your code below this line - +function isPalindrome (word) { + if (word === reverseWord(word)) { + return true + } else { + return false + } +} + +function reverseWord (fwd) { + const str = fwd.split('') + const reverseStr = str.reverse() + const reversedWord = reverseStr.join('') + + return reversedWord +} + +console.log(isPalindrome('run')) // Add your code above this line /** added for formatting purposes **/ @@ -208,6 +318,30 @@ console.log('Problem 5:') // Add your code below this line +function doubleLetters (word) { + const wordLetters = word.split('') + let hasDoubleLetters = false + + // iterate through letters of the submitted word + wordLetters.forEach((letter, index) => { + // declare the current letter and previous letter of the iteration + const currentLetter = wordLetters[index] + const previousLetter = wordLetters[index - 1] + + // debug + // console.log(`current letter is ${currentLetter}. previous letter was ${previousLetter}.`) + + // if they're the same, then double letters are present, + // return true + if (currentLetter === previousLetter) { + hasDoubleLetters = true + } + }) + + return (hasDoubleLetters) +} + +console.log(doubleLetters('apple')) // Add your code above this line /** added for formatting purposes **/