Skip to content

Commit

Permalink
Merge pull request #93 from Capstone-Projects-2024-Spring/reizas-dev2
Browse files Browse the repository at this point in the history
Fixed wpm calculation bug
  • Loading branch information
icycoldveins authored May 2, 2024
2 parents f8c0292 + 1cb7e03 commit f47124d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
2 changes: 1 addition & 1 deletion static/js/reusable/DynamicMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function DynamicMode() {
currentWordIndex = 0; //initializes value for play again
currentCharIndex = 0;
correctCharsTyped = 0; //Need to reset to prevent other games from using previous numbers
correctLettersTyped=0;
totalCharsTyped = 0;
currBlurbIndex = 0;
userInputCorrectText = "";
Expand Down Expand Up @@ -232,7 +233,6 @@ function DynamicMode() {
const endTime = new Date().getTime();
const elapsedTime = timeToType / 1000;
const wordsPerMinute = Math.round((correctLettersTyped / 5.0) / (elapsedTime / 60.0));
console.log(correctCharsTyped);
let accuracy = 0;
if (totalCharsTyped!=0){
accuracy = (correctCharsTyped / totalCharsTyped) * 100 ;
Expand Down
1 change: 1 addition & 0 deletions static/js/reusable/RobotOpponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ function RobotOpponent() {
currentWordIndex = 0; //initializes value for play again
currentCharIndex = 0;
correctCharsTyped = 0; //Need to reset to prevent other games from using previous numbers
correctLettersTyped = 0;
totalCharsTyped = 0;
userInputCorrectText = "";
document.getElementById("input-box").value = "";
Expand Down
60 changes: 24 additions & 36 deletions static/js/reusable/SinglePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ function ThrillTyperGame() {
// Default genre to null if not passed
currentWordIndex = 0; // Initializes value for play again
currentCharIndex = 0;
correctCharsTyped = 0;
correctLettersTyped = 0;
totalCharsTyped = 0;
userInputCorrectText = "";
document.getElementById("input-box").value = "";
document.getElementById("result").innerHTML = "";
Expand Down Expand Up @@ -196,42 +199,26 @@ function ThrillTyperGame() {
}

function checkInput() {
var userInputText = document.getElementById("input-box").value;
var userInputLastChar = userInputText[userInputText.length - 1];

//updates text color
updateText();

//idk what this is
//if typed word matches with text word and last letter is space, clear input box and add word to userInputCorrectText
if (userInputText.substring(0, userInputText.length - 1) == words[currentWordIndex] && userInputLastChar == ' ') {
currentWordIndex++;
userInputCorrectText += userInputText; //saves correct text
document.getElementById("input-box").value = "";
}
var userInputText = document.getElementById("input-box").value;
var userInputLastChar = userInputText[userInputText.length - 1];

if (userInputLastChar == text[currentCharIndex]) { //works but logic is bad
currentCharIndex++;
correctCharsTyped++;
if(text[currentCharIndex]!=' '){
correctLettersTyped++;
}
}
totalCharsTyped++;
//updates text color
updateText();

//idk what this is
//if typed word matches with text word and last letter is space, clear input box and add word to userInputCorrectText
if (
userInputText.substring(0, userInputText.length - 1) ==
words[currentWordIndex] &&
userInputLastChar == " "
) {
currentWordIndex++;
userInputCorrectText += userInputText; //saves correct text
document.getElementById("input-box").value = "";
if (userInputText.substring(0, userInputText.length - 1) == words[currentWordIndex] && userInputLastChar == ' ') {
currentWordIndex++;
userInputCorrectText += userInputText; //saves correct text
document.getElementById("input-box").value = "";
}
if (userInputLastChar == text[currentCharIndex]) {
//works but logic is bad
currentCharIndex++;
correctCharsTyped++;

if (userInputLastChar == text[currentCharIndex]) { //works but logic is bad
currentCharIndex++;
correctCharsTyped++;
if(text[currentCharIndex]!=' '){
correctLettersTyped++;
}
}
totalCharsTyped++;
}
Expand All @@ -240,7 +227,7 @@ function ThrillTyperGame() {
const currentTime = new Date().getTime();
const timeLeft = (timeLimit-(currentTime - startTime)) / 1000;
document.getElementById("result").innerHTML = `Time elapsed: ${timeLeft.toFixed(0)} seconds`;
if(timeLeft<=0){
if(timeLeft<=0 || currentCharIndex>=text.length){
submitInput();
}
}
Expand All @@ -251,6 +238,9 @@ function ThrillTyperGame() {
document.getElementById("input-box").value = "";
userInputCorrectText = "";
currentCharIndex = 0;
correctCharsTyped = 0;
correctLettersTyped = 0;
totalCharsTyped = 0;
document.getElementById("result").innerHTML = "";
currentWordIndex = 0; //initializes value for play again
updateText();
Expand Down Expand Up @@ -299,8 +289,6 @@ function ThrillTyperGame() {
stopTimerInterval();
const endTime = new Date().getTime();
const elapsedTime = (endTime - startTime) / 1000;
console.log(elapsedTime);
console.log(correctCharsTyped);
const wordsPerMinute = Math.round((correctLettersTyped / 5.0) / (elapsedTime / 60.0));
const accuracy = (correctCharsTyped / totalCharsTyped) * 100;
document.getElementById("result").innerHTML = `Congratulations! You completed the game in ${elapsedTime.toFixed(2)} seconds. Your speed: ${wordsPerMinute} WPM. Your accuracy: ${accuracy.toFixed(2)}%`;
Expand Down

0 comments on commit f47124d

Please sign in to comment.