-
Notifications
You must be signed in to change notification settings - Fork 12
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
Completed Homework Assignment #2 - Caleb Sanchez #1
base: master
Are you sure you want to change the base?
Conversation
cswebdesign
commented
Sep 19, 2019
- Produced the winning result for two-player Blackjack and returned 0 when both players lost
- Printed an array with total word count occurrences from user input
- Printed Scrabble score based on user word input
- Tested user word input to see if it was a palindrome
- Tested user word input to see if it had two consecutive identical letters and printed True or False
function blackJack(playerCardScore, dealerCardScore) { | ||
if (playerCardScore > dealerCardScore && playerCardScore <= 21) { | ||
return playerCardScore | ||
} else if (playerCardScore < dealerCardScore && dealerCardScore <= 21) { | ||
return dealerCardScore | ||
} else if (playerCardScore < dealerCardScore && playerCardScore <= 21) { | ||
return playerCardScore | ||
} else if (playerCardScore === 21 && dealerCardScore === 21) { | ||
return 21 | ||
} else { | ||
return 0 | ||
} | ||
} | ||
|
||
console.log(blackJack(19, 21)) | ||
|
||
// Add your code above this line |
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.
@cswebdesign Good Job her on this solution you have here. The function is good and you did a good job using the conditionals statements to get your outcome for the game.
Take a look at this solution here
function wordCount(phrase) { | ||
//Defining the Word array | ||
const cleanedPhrase = phrase.replace(/[,]/g, '') | ||
//Defining blank array for later use | ||
const wordArray = {} | ||
|
||
//Looping through array and assigning number of instances to each element | ||
cleanedPhrase.split(" ").forEach(function(currentValue, i, array) { | ||
wordArray[currentValue] = wordArray[currentValue]? ++ wordArray[currentValue]: 1 | ||
}) | ||
//Printing final array | ||
return wordArray | ||
} | ||
|
||
const finalPhrase = wordCount("Baby shark, doo doo doo doo doo doo") | ||
console.log(finalPhrase) | ||
|
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.
@cswebdesign Great work here with this solution. Nice job using the conditional ternary operator to increment the object value by 1 if it exists and creating the key and setting the value to 1 if it doesn't.
Take a look at this solution here
|
||
//Global variable for user inputted word | ||
const userInput = 'run' | ||
|
||
//Create variable and reverse user inputted word | ||
function reverseString(word) { | ||
//Turn the word into individual letters in an array | ||
const splitString = word.split('') | ||
//Reverse the array elements | ||
const reverseArray = splitString.reverse() | ||
//Combine the array elements back into a word | ||
const joinArray = reverseArray.join('') | ||
return joinArray | ||
} | ||
|
||
//Global variable that holds reversed word | ||
const reversedString = reverseString(userInput) | ||
|
||
//Comparing original word to reversed word and returning True or False | ||
function isPalindrome(word) { | ||
if(word === reversedString) { | ||
return "True" | ||
} else { | ||
return "False" | ||
} | ||
} | ||
|
||
const qualityCheck = isPalindrome(userInput) | ||
console.log(`Is your word input a palindrome? ${qualityCheck}.`) | ||
|
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.
@cswebdesign Awesome Job with this solution. Good Idea separating out your logic into separate functions that you can reuse. One suggestion, with your global variablereversedString
you can move them inside your isPalindrome function, replace the userInput parameter with word
, and make it a local variable. Also in your return "True" and "False" statements you can remove the quotes and make it lowercase. this will turn the value from a string to a boolean.
Take a look at this solution here
|
||
function doubleLetters(word) { | ||
//Converted word to individual letters array | ||
const splitWord = word.split('') | ||
//Created false variable as finalCheck kept displaying undefined | ||
const notFound = false | ||
//Checks each letter to see if it matches the consecutive element | ||
for ( i = 0; i < splitWord.length; i++ ) { | ||
if(splitWord[i] === splitWord[i + 1]) { | ||
return true | ||
} | ||
} | ||
//Returns false if for loop produces no results | ||
return notFound | ||
} | ||
|
||
const finalCheck = doubleLetters('apple') | ||
|
||
console.log(finalCheck) | ||
|
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.
@cswebdesign Good work on this problem here. Good work looping through each letter and checking for that repeating value with the conditional you have here. One suggestion you can remove the variable notFound
and you can just return false
on line 321
Take a look at the solution here
// Add your code below this line | ||
|
||
//Takes user input and calculates points | ||
function scrabbleScore(word) { | ||
//Using split to convert user input to array of individual letters | ||
const splitLetters = word.split('') | ||
|
||
//For loop that iterates through each array element and assigns a point value | ||
for (i = 0; i < splitLetters.length + 1; i++) { | ||
//If statement that assigns number to array element based on criteria | ||
if (splitLetters[i] === 'a' || splitLetters[i] === 'e' || splitLetters[i] === 'i' || splitLetters[i] === 'o' || splitLetters[i] === 'u' || splitLetters[i] === 'l' || splitLetters[i] === 'n' || splitLetters[i] === 'r' || splitLetters[i] === 's' || splitLetters[i] === 't') { | ||
splitLetters[i] = 1 | ||
} else if (splitLetters[i] === 'd' || splitLetters[i] === 'g') { | ||
splitLetters[i] = 2 | ||
} else if (splitLetters[i] === 'b' || splitLetters[i] === 'c' || splitLetters[i] === 'm' || splitLetters[i] === 'p') { | ||
splitLetters[i] = 3 | ||
} else if (splitLetters[i] === 'f' || splitLetters[i] === 'h' || splitLetters[i] === 'v' || splitLetters[i] === 'w' || splitLetters[i] === 'y') { | ||
splitLetters[i] = 4 | ||
} else if (splitLetters[i] === 'k') { | ||
splitLetters[i] = 5 | ||
} else if (splitLetters[i] === 'j' || splitLetters[i] === 'x') { | ||
splitLetters[i] = 8 | ||
} else if (splitLetters[i] === 'q' || splitLetters[i] === 'z') { | ||
splitLetters[i] = 10 | ||
} else { | ||
//Adds up the converted numbers to get total points | ||
const add = (a, b) => a + b | ||
const totalSum = splitLetters.reduce(add) | ||
//console.log(totalSum) | ||
//Returns the total points | ||
return totalSum | ||
} | ||
} | ||
} | ||
|
||
const finalScore = scrabbleScore("cabbage") | ||
console.log(`Expected Result: ${finalScore}`) | ||
|
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.
@cswebdesign Good Work here with this solution. Good use of the conditionals to compare each letter of the work with the letters of the alphabet and store the value in an array. Good job using the reduce
method to add the points for each letter and return that value
Take a look at an alternate solution here