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

Abhash's commit #13

Open
wants to merge 1 commit into
base: s1-first-cut
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
75 changes: 62 additions & 13 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,69 @@ function registerGuess(guess) {
guess = guess.toUpperCase();
const status = [];
const WORD_LETTERS = WORD.split("");
guess.split("").forEach(function(letter, index) {
// TODO: handle additional letters when there are duplicates
let letterStatus;
const existsInWord = WORD_LETTERS.indexOf(letter) > -1;
const isInPlace = WORD_LETTERS[index] === letter;
if (isInPlace) {
letterStatus = 2;
} else if (existsInWord) {
letterStatus = 1;
} else {
letterStatus = 0;


// guess.split("").forEach(function(letter, index) {
// // TODO: handle additional letters when there are duplicates
// let letterStatus;
// const existsInWord = WORD_LETTERS.indexOf(letter) > -1;
// const isInPlace = WORD_LETTERS[index] === letter;
// if (isInPlace) {
// letterStatus = 2;
// } else if (existsInWord) {
// letterStatus = 1;
// } else {
// letterStatus = 0;
// }
// status.push(letterStatus);
// })

// function to get frequency of each word in string
function getFrequency(string) {
var freq = {};
for (var i=0; i<string.length;i++) {
var character = string.charAt(i);
if (freq[character]) {
freq[character]++;
} else {
freq[character] = 1;
}
}

return freq;
};


//calling the function
var freq = getFrequency(WORD);


for(let i=0;i<guess.length;i++){
if(guess[i]===WORD[i])
{
console.log("Matched");
freq[guess[i]]--;
status[i]=2;
}

}
for(let i=0;i<guess.length;i++){
if (!status[i]) {
status[i]=0;
}
let exists = WORD_LETTERS.indexOf(guess[i])>-1;
if(guess[i]!==WORD[i] && exists && freq[guess[i]]>0)
{
freq[guess[i]]--;
status[i]=1;
}
status.push(letterStatus);
})


}

for(i=0;i<status.length;i++)
console.log(status[i]);

printGuess(guess, status);
return status;
}
Expand Down