I'm currently working on developing a hangman game in JavaScript (I am new to web technologies) and I have come across my first challenge. The placeholder string for the word to be guessed, which consists of hyphens and spaces, is overflowing from the containing div.
For instance
If there are 7 dash placeholders at the end of the line, it breaks into 6 dashes staying on the top line and one dash moving to the bottom line.
This display looks messy. How can I prevent this issue and keep my guessing sentence as one continuous string?
var secretWord = 'A random text you need to guess without breaking in the middle of a word';
secretWord = word.toUpperCase();
var hiddenWord = '';
var lettersAvailable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for (i = 0; i < secretWord.length; i++)
{
if (secretWord.charAt(i) != ' ') hiddenWord += '-';
else hiddenWord += ' ';
}
function showHiddenPart() {
document.getElementById('WordBox').innerHTML = hiddenWord;
}
showHiddenPart();
window.onload = begin;
function begin(){
var content_div = '';
for(i = 0; i < 35; i++)
{
var element_code = 'l'+i;
content_div += '<div class="letter" onclick="check('+i+')" id="'+element_code+'">'+lettersAvailable.charAt(i)+'</div>';
}
document.getElementById('alfabet').innerHTML = content_div;
showHiddenPart();
}
String.prototype.UpdateChar = function(position, character) {
if (position > this.length - 1) return this.toString();
else return this.substr(0, position) + character + this.substr(position+1);
}
function check(number) {
var selected = false;
for(i = 0; i < secretWord.length; i++)
{
if (secretWord.charAt(i) == lettersAvailable.charAt(number)) {
hiddenWord = hiddenWord.UpdateChar(i, lettersAvailable.charAt(number));
selected = true;
}
}
if (selected == true){
var identifier = 'l'+number;
document.getElementById(identifier).style.background = "#003300";
document.getElementById(identifier).style.color = "#00C000";
document.getElementById(identifier).style.border = "3px solid #00C000";
document.getElementById(identifier).style.cursor = "default";
document.getElementById(identifier).style.boxShadow = "none";
showHiddenPart();
}
}
#container
{
margin-left: auto;
margin-right: auto;
margin-top: 5em;
display: grid;
grid-template-columns: 1fr 1fr;
width: 900px;
}
#WordBox
{
grid-area: 1 / 1 / 1 / 3;
text-align: center;
font-size: 2.4em;
min-height: 100px;
}
#alfabet
{
grid-area: 2 / 2 / 3 / 3;
min-height: 280px;
display: grid;
grid-template-columns: repeat(7, auto);
grid-row-gap: .5em;
grid-column-gap: .5em;
justify-content: center;
align-items: center;
}
.letter
{
width: 30px;
height: 30px;
text-align: center;
padding: 5px;
border: 3px solid gray;
cursor: pointer;
border-radius: 12px;
}
<div id="container">
<div id="WordBox"></div>
<div id="alfabet"></div>
</div>
If any important part of the code has been missed out, please let me know. I appreciate any assistance as I have not been able to find a solution through online searches.