I'm trying to create a hangman game. I need to replace the underscores in the variable "underscore" with the letter in the variable "letter" when the letter matches what's in the variable "word". Any way of doing this?
for( i = 0; i < word.length; i++ ) {
if (word[i] === letter) {
underscore = underscore.replace(underscore.charAt(i*2), letter);
}
}
Above is the specific part of my code that I have trying to replace it at the moment but it will only replace the next underscore after a letter/beginning no matter what I do. Below is my full code if you need any more details of how it works.
Thanks in advance.
/* Hangman Game CSS File */
body
{
background-color: #1B264F;
}
#main {
background-color: #5386E4;
height: 97vh;
width: 95vw;
vertical-align: middle;
}
canvas {
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
margin-top: auto;
margin-bottom: auto;
}
<!DOCTYPE html>
<html>
<head>
<title>Hangman by Lachlan Teale</title>
<meta name="description" content="Lachlan Teale's Hangman Game 2015">
<meta name="keywords" content="hangman, lachlan, teale, Javascript">
<meta charset="UTF-8">
<meta name="author" content="Lachlan Teale">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<canvas id="main" width="1920" height="1080" style="border:1px solid #d3d3d3;"></canvas>
<script type="text/javascript">
//setting variables
var canvas = document.getElementById('main');
var context = canvas.getContext('2d');
var underscore = "";
var letter;
var complete = false;
//list of words
var wordList = ["apple", "peach", "pear", "mango", "banana", "paper", "pens", "laptop", "desk", "chair", "star", "sky", "moon", "sun", "planets"];
//picking a random word from the list
var word = wordList[Math.floor(Math.random() * wordList.length)];
//print out the underscores for the number of letters
for (i = 0; i < word.length; i++) {
var underscore = underscore + "_ ";
}
context.font = '50pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.fillText(underscore, canvas.width * 0.5, canvas.height * 0.70);
//correct letter text
context.font = 'bold 30pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.text
context.fillText("Correct Letters", canvas.width * 0.08, canvas.height * 0.05);
//Wrong Letter text
context.font = 'bold 30pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.text
context.fillText("Wrong Letters", canvas.width - (canvas.width * 0.08), canvas.height * 0.05);
//checking which button is pressed
window.addEventListener('keydown', function(event) {
if ( event.keyCode >= 65 && event.keyCode <= 90 ) {
context.clearRect(0, canvas.height * 0.80, canvas.width, canvas.height);
letter = String.fromCharCode( event.keyCode ).toLowerCase();
context.font = '50pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.fillText(letter, canvas.width * 0.5, canvas.height * 0.85);
}
});
//checking if back key is pressed
document.addEventListener('keydown', function(event) {
if ( event.keyCode == 8 ) {
context.clearRect(0, canvas.height * 0.80, canvas.width, canvas.height);
}
});
//checking if enter is pressed
document.addEventListener('keydown', function(event) {
if( event.keyCode == 13 ) {
//checking if letter is in the word
alert(word);
for( i = 0; i < word.length; i++ ) {
if (word[i] === letter) {
underscore = underscore.replace(underscore.charAt(i*2), letter);
}
}
context.clearRect(canvas.width * 0.4, canvas.height * 0.3, canvas.width *0.7, canvas.height * 70);
context.font = '50pt Calibri';
context.textAlign = 'center';
context.fillStyle = '#E2E2E3';
context.fillText(underscore, canvas.width * 0.5, canvas.height * 0.70);
alert(underscore);
}
});
</script>
</body>
</html>