Skip to content

JavaScript Adagram#143

Open
DQLIU1995 wants to merge 1 commit intoAda-C18:mainfrom
DQLIU1995:main
Open

JavaScript Adagram#143
DQLIU1995 wants to merge 1 commit intoAda-C18:mainfrom
DQLIU1995:main

Conversation

@DQLIU1995
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@anselrognlie anselrognlie left a comment

Choose a reason for hiding this comment

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

🎉 Nice job tackling adagrams using JavaScript! I've left comments throughout, so please take a look and let me know in Slack or our next 1:1 if there's anything you'd like to follow up on. Take a look at usesAvailableLetters in particular.

Comment thread src/adagrams.js
export const drawLetters = () => {
// Implement this method for wave 1
};
const letter_count = {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider defining letter_count outside the function to focus on the code. Your code already does the work of making a copy of data when building the expanded letter list, so relocating it won't be a problem.

Comment thread src/adagrams.js
const drawn = [];

const keyList = Object.keys(letter_count);
for(let key of keyList) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👀 Prefer const for for/of loops.

Comment thread src/adagrams.js
Comment on lines +37 to +41
let frequency = 0;
while(frequency < letter_count[key]){
letter_bank.push(key)
frequency ++
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We know how many times this loop should run, so prefer using a for loop to a while loop.

    for (let i = 0; i < letter_count[key]; i += 1) {
      letter_bank.push(key)
    }; 

Comment thread src/adagrams.js
};

for(let i = 0; i < 10; i++) {
let select_char = Math.floor(Math.random() * (letter_bank.length -1));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

random is exclusive on the upper end, so subtracting 1 from the size will never pick the final location. Also, prefer const here.

    const select_char = Math.floor(Math.random() * (letter_bank.length));

This logic selects letters with equal weighting. You have logic to reject invalid letters (letters we've already used up), but be aware that this will tend to select hands with an overrepresentation of "hard" letters, which would make it harder for the player to form words.

Comment thread src/adagrams.js
for(let i = 0; i < 10; i++) {
let select_char = Math.floor(Math.random() * (letter_bank.length -1));
drawn.push(letter_bank[select_char]);
letter_bank.splice(select_char, 1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👀splice like python's remove is linear with respect to the length of the list. This won't be a problem for the size of data we're working with, but consider using a strategy of virtually "dividing" the list into a used and unused side. Swapping a value to the used side would be constant time!

Comment thread src/adagrams.js

let total_score = 0 ;

for(let rawChar of word) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prefer const tolet

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

for isn't a function, so the () should stick directly to it. Leave a space after for and other control structure keywords (like if and while)

Comment thread src/adagrams.js
let total_score = 0 ;

for(let rawChar of word) {
let char = rawChar.toUpperCase();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

const again. Generally always try to use const and only switch back to let if JS tells us we have to (if we really do need to change what the variable refers to).

Comment thread src/adagrams.js

for(let rawChar of word) {
let char = rawChar.toUpperCase();
if(Object.keys(score_card).includes(char)){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👀 This will iterate over all the keys of the score chart (building a new list), then iterating through the list to look for char. Instead, we can use some variation of in, or hasOwnKey to lookup a key in an object. Or, we can try to access a key, and use falsiness (from undefined) to provide a default value such as 0.

Comment thread src/adagrams.js
Comment on lines +96 to +102
let score = scoreWord(each_word)
if (score in scoreObject) {
scoreObject[score].push(each_word)
}
else {
scoreObject[score] = [each_word]
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 Nice way to build up lists of tied words!

Comment thread src/adagrams.js
if (scoreObject[maxScore].length > 1) {
if (wordToChoose.length !== 10) {
for (let each_word of scoreObject[maxScore]) {
if (each_word.length < wordToChoose.length) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's a little surprising to no have the 10-letter check here as well, but the logic checks out. If the first word is 10-letter, we'll never get here. If any other word is 10-letter, we'll exit the loop. Consider teasing apart some of this logic for clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants