Note that there are some explanatory texts on larger screens.

plurals
  1. POExplanation of a function that gets letter from word
    primarykey
    data
    text
    <p>I'm making a hangman game in javascript, and I'm having trouble understanding the code in this one function.</p> <p>Here is the function:</p> <pre><code>function getLetter(word,letter,display){ // This method is called by the Hangman program when your isLetterInWord function // above returns true. // The parameters passed in are the guessed letter, the secret word, // and the current display state of the secret word. // This method will return a new display state of the secret word based on the matching letter. // REPLACE THIS CODE WITH YOUR getLetter() METHOD while (word.search(letter) != -1) { var index=word.search(letter) display = display.substr(0, index) + letter + display.substr(index + 1); word = word.substr(0, index) + '-' + word.substr(index + 1); } return display; } </code></pre> <p>The part I don't really understand:</p> <pre><code>display = display.substr(0, index) + letter + display.substr(index + 1); word = word.substr(0, index) + '-' + word.substr(index + 1); </code></pre> <p>Basically this program takes a word, find the number of letters and replaces them with hyphens. For example the word 'boat' would be turned to '----'</p> <p>The the job of the function above is to replace the letter guess (correctly) with the corresponding hyphen.</p> <p>Here is the background code for the whole project.</p> <pre><code>// Hangman Project //RETURN A 'HIDDEN' VERSION OF THE SUPPLIED SECRET WORD function getDisplay(word) { // Given a string, "word", return a hidden version of it consisting // of dashes for the display. // REPLACE THIS CODE WITH YOUR getDisplay() METHOD var disp=""; for (var i=0; i &lt; word.length; i++ ){ disp = disp +'-'; } return disp; } //FIND IF THE LETTER IS IN THE WORD function isLetterInWord(word,letter){ // Given the word "word", check if it contains the letter "letter". // REPLACE THIS CODE WITH YOUR isLetterInWord() METHOD if(word.search(letter) != -1) { return true; } else { return false; } } </code></pre> <p>Any help explaining those two lanes would be appreciated. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload