Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js hangman with repeated characters

var secretWord = 'apple',
    letters    = '';

function play(letter) {
  if(letters.indexOf(letter) != -1) {
    console.log("You've already tried '" + letter + "' ...");
  }
  else {
    letters += letter;

    if(secretWord.indexOf(letter) == -1) {
      console.log(letter + ' -> wrong!');
    }
    else {
      var progress = secretWord.replace(new RegExp('[^' + letters + ']', 'g'), '_');

      console.log(letter + ' -> ' + progress);
      
      if(progress == secretWord) {
        console.log('Well done!');
      }
    }
  }
}

play('i');
play('e');
play('p');
play('e');
play('a');
play('l');
Comment

PREVIOUS NEXT
Code Example
Javascript :: variables 2 python .Bartolome sintes Marco 
Javascript :: window.initMochaPhantomJS(); 
Javascript :: payfast javascript integration 
Javascript :: react createElement interactive button 
Javascript :: what is the purpose of subscript in an array 
Javascript :: chrome extension detect second monitor 
Javascript :: copy two fields to one javascript 
Javascript :: tailwindcss with django 
Javascript :: como fazer map em javascript 
Javascript :: syntax to call item from array 
Javascript :: new activexobject( adodb.connection ) javascript 
Javascript :: use vue cdn with firestore 
Javascript :: show object unordered in chrome console 
Javascript :: add object to array setstate 
Javascript :: javascript picture delete after time 
Javascript :: javascript file access to resources asp.net mvc 
Javascript :: passing variable in table row delete button node js express 
Javascript :: js resize div with mouse 
Javascript :: quasar electron getPath 
Javascript :: solr regex expression 
Javascript :: how to make a rectangle in matter.js 
Javascript :: javascript see if chrome is in dark mode 
Javascript :: js null vs undefine 
Javascript :: snippets chrome devtools debugging 
Javascript :: {backgroundimage: `url("${require(`../../assets/images/${post.image}`)}")`}; 
Javascript :: js event problem solving 
Javascript :: switch variable scope js 
Javascript :: use node-session with serverless functions 
Javascript :: native base change track color 
Javascript :: hidden vue js 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =