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

duplicate problem solve #14

Open
wants to merge 4 commits into
base: main
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
36 changes: 35 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<body>
<header>
<div id="center-content">
<!-- <button>?</button> -->
<!-- <button value="">?</button> -->
<h1>WORDLE</h1>
</div>
</header>
Expand All @@ -31,6 +31,40 @@ <h1>WORDLE</h1>
<div class="letter"></div>
</div>
<input id="guess" value="" maxlength="5" />
<div class="keyboard-container">
<div class="keyboard-first-row">
<button value="Q">Q</button>
<button value="W">W</button>
<button value="E">E<Emb></Emb></button>
<button value="R">R</button>
<button value="T">T</button>
<button value="Y">Y</button>
<button value="U">U</button>
<button value="I">I</button>
<button value="O">O</button>
<button value="P">P</button>
</div>
<div class="keyboard-second-row">
<button value="A">A</button>
<button value="S">S</button>
<button value="D">D</button>
<button value="F">F</button>
<button value="G">G</button>
<button value="H">H</button>
<button value="J">J</button>
<button value="K">K</button>
<button value="L">L</button>
</div>
<div class="keyboard-third-row">
<button value="Z">Z</button>
<button value="X">X</button>
<button value="C">C</button>
<button value="V">V</button>
<button value="B">B</button>
<button value="N">N</button>
<button value="M">M</button>
</div>
</div>
</main>
</body>
<script src="js/lib/words.js"></script>
Expand Down
76 changes: 62 additions & 14 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,76 @@
let attempts = 6;

let userInput="";
console.log("Hello")
const WORD = WORDS[Math.floor(Math.random() * WORDS.length)];
const el = document.querySelector("#guess");
const KeyboardBtn = document.querySelectorAll("button");


KeyboardBtn.forEach((key)=>{
key.addEventListener("click",function(e){
userInput+=e.target.value;
drawGhostInput(userInput);
if (userInput.length === 5) {
drawGhostInput(""); // setting the new input to 0
const result = registerGuess(userInput);
userInput="";
e.target.value = "";
const event = new Event('input');
e.target.dispatchEvent(event);
const reducer = (previousValue, currentValue) => previousValue + currentValue;
if (result.reduce(reducer) === 10) {
el.classList.add("hidden");
const victoryMessage = document.createElement("div");
victoryMessage.innerText = "You won";
document.body.appendChild(victoryMessage);
}
console.l
} else {
console.log("Skip this");
}
})
})

console.log("Target:", WORD);

function registerGuess(guess) {
let counter = new Map();

for(let i=0;i<WORD.length;i++)
{
counter[WORD[i]]=0;
}

for(let i=0;i<WORD.length;i++)
{
counter[WORD[i]]++;
}

guess = guess.toUpperCase();

for(let i=0;i<guess.length;i++){
if(guess[i]==WORD[i]) // INPLace checking
counter[guess[i]]--;
}

const status = [];
const WORD_LETTERS = WORD.split("");
guess.split("").forEach(function(letter, index) {
// TODO: handle additional letters when there are duplicates
for(let i=0;i<guess.length;i++){
console.log(guess[i])
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;
if(guess[i]==WORD[i]){
letterStatus=2;
}
status.push(letterStatus);
})
else if(counter[guess[i]]>0){
letterStatus=1;
counter[guess[i]]--;
}
else{
letterStatus=0;
}
console.log(letterStatus)
status.push(letterStatus)
}

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