Skip to content

Branches - Farah #36

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
142 changes: 141 additions & 1 deletion src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,148 @@
const Adagrams = {
allLettersHash: {
'A': 9,
'B': 2,
'C': 2,
'D': 4,
'E': 12,
'F': 2,
'G': 3,
'H': 2,
'I': 9,
'J': 1,
'K': 1,
'L': 4,
'M': 2,
'N': 6,
'O': 8,
'P': 2,
'Q': 1,
'R': 6,
'S': 4,
'T': 6,
'U': 4,
'V': 2,
'W': 2,
'X': 1,
'Y': 2,
'Z': 1,
},

drawLetters() {
// Implement this method for wave 1
let allLettersArray = [];
for (let key in this.allLettersHash) {
for (let i = 0; i < Number(this.allLettersHash[key]); i++) {

Choose a reason for hiding this comment

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

In general I suggest avoiding use of the old-fashioned for loop in favor of alternative loops. That said, using it for a counter-driven loop like this is ok.

allLettersArray.push(key);
}
};

let drawnLetters = [];
for (let i = 0; i < 10; i++) {
let index = Math.floor((Math.random() * allLettersArray.length));
drawnLetters.push(allLettersArray[index]);
allLettersArray.splice(index,1);
};

return drawnLetters
},

usesAvailableLetters(word,drawnLetters) {
word = word.toUpperCase().split('');
let lettersInHand = [...drawnLetters];

for (let i = 0; i < word.length; i++) {
if (lettersInHand.includes(word[i])) {
let wordLocation = lettersInHand.indexOf(word[i]);
lettersInHand.splice(wordLocation,1);
} else {
return false;
};
};

return true;
},

scoreWord(word) {
word = word.toUpperCase().split('');
let totalScore = 0;
for(let i = 0; i < word.length; i++) {
if (word[i] === 'A' || word[i] === 'E' || word[i] === 'I' || word[i] === 'O' || word[i] === 'U' || word[i] === 'L' || word[i] === 'N' || word[i] === 'R' || word[i] === 'S' || word[i] === 'T') {

Choose a reason for hiding this comment

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

You could do better here with a data structure like a JS object. They key being the letter and the value being the score. The way you did this is a little awkward.

totalScore += 1;
} else if (word[i] === 'D' || word[i] === 'G') {
totalScore += 2;
} else if (word[i] === 'B' || word[i] === 'C' || word[i] === 'M' || word[i] === 'P') {
totalScore += 3;
} else if (word[i] === 'F' || word[i] === 'H' || word[i] === 'V' || word[i] === 'W' || word[i] === 'Y') {
totalScore += 4;
} else if (word[i] === 'K') {
totalScore += 5;
} else if (word[i] === 'J' || word[i] === 'X') {
totalScore += 8;
} else if (word[i] === 'Q' || word[i] === 'Z') {
totalScore += 10;
};
};

if (word.length > 6) {
totalScore += 8
};

return totalScore;
},

highestScoreFrom(words) {
let winningWord = {};
let winningWordScore = 0;

let scoreHash = {};
let that = this;

words.forEach( function(word) {
scoreHash[word] = that.scoreWord(word);
});

// for (let key in scoreHash) {
// console.log(`******${key}:${scoreHash[key]}************`);
// };

let maxValue = Math.max(...Object.values(scoreHash));
// console.log(...Object.values(scoreHash));
// console.log(`**********${maxValue}**********`);

let tie = {};

for (let key in scoreHash) {
if (scoreHash[key] === maxValue) {
tie[key] = scoreHash[key];
};
};

let shortestWord = "xxxxxxxxxx";

for (let key in tie) {
if (key.length === 10) {
winningWord = {
'word': key,
'score': tie[key],
};
return winningWord;
} else if (key.length < shortestWord.length) {
shortestWord = key;
winningWordScore = tie[key]
};
};

winningWord = {
'word': shortestWord,
'score': winningWordScore,
};

return winningWord;
},

};

// Do not remove this line or your tests will break!
export default Adagrams;

console.log(Adagrams.drawLetters());
2 changes: 1 addition & 1 deletion test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('Adagrams', () => {
});
});

describe.skip('highestScoreFrom', () => {
describe('highestScoreFrom', () => {
it('returns a hash that contains the word and score of best word in an array', () => {
const words = ['X', 'XX', 'XXX', 'XXXX'];
const correct = { word: 'XXXX', score: Adagrams.scoreWord('XXXX') };
Expand Down