0

My code is a version of an old game Mastermind and is supposed to guess my secret code, and if user guesses the right character it prints letter green, if its right character wrong place it prints out red and black if completely wrong.

 <!doctype.html>
<html>
<head>
  <title> Mastermind </title>
<script>
  var secret= 'FLGRL';
  function init() {
    var button = document.getElementById('startButton');
    button.onclick = myButtonClick;
  }
  function myButtonClick() {
    var userTry = document.getElementById('userGuess').value;
    var ul = document.getElementById('guessList');
    var li = document.createElement('li');
  }
    for (var i=0; i < secret.length; i++) {
      var found = false;
      //if user finds right character, right spot
      if (userTry.charAt(i)===(secret.charAt(i))) {
        //if code and guess are a match, display letter in greeting
        li.innerHTML += userTry.charAt(i).fontcolor('green');
        found = true;
        //show user
      }
      //if character is in the code but in wrong
      else if {
            for (var j=0; j = charAt('secret'); j++) {
              var found = true;
              if (userTry.charAt(j)===(secret.charAt(j))){
                li.innerHTML += userTry.charAt(j).fontcolor('red');
              }

      if (found === false) {
        li.innerHTML += userTry.charAt(i). fontcolor('black');
        //show user character is wrong

      }
    }
    ul.appendChild(li);
    alert(userTry);

  window.onload = init;
  </script>
  </head>
  <body>
  <form>
  <input type="text" id="userGuess" size="15" placeholder="Your Guess">
  <input type="button" id="startButton" value="Place your Guess">
  </form>
  <ul id="guessList">
  </ul>
  </body>
  </html>
5
  • What is the question? Commented Feb 23, 2018 at 4:33
  • Ok so this is my code now, but it is not running at all when I open it in my browser and I've been trying to debug it myself but I am driving myself crazy @HubertSchölnast
    – Dallas
    Commented Feb 23, 2018 at 4:45
  • @HubertSchölnast i've updated my code to what I've been working on since this post
    – Dallas
    Commented Feb 23, 2018 at 4:45
  • "It drives my crazy" is not a question. What exactly do you want to know? What exactly is going wrong? How did you try to fix it? What happened when you tried? What did you expect? Commented Feb 23, 2018 at 4:47
  • Go to stackoverflow.com/help/on-topic and read item #1. Commented Feb 23, 2018 at 4:50

1 Answer 1

0

the code has many mistakes, at least the code you posted. One not closed bracket and also this is wrong

else if { Blockquote

The if is unnecessary or is missing the condition. More, what is this ?

 for (var j=0; j = charAt('secret'); j++) 

The condition is wrong.

I've corrected this things, the code is not still 100% corrected but :

 <!doctype.html>
   <html>

   <head>
     <title> Mastermind </title>
     <script>
       var secret = 'FLGRL';

       function init() {
         var button = document.getElementById('startButton');
         button.onclick = myButtonClick;
       }
       var userTry;

       function myButtonClick() {
         userTry = document.getElementById('userGuess').value;
         var ul = document.getElementById('guessList');
         var li = document.createElement('li');


         for (var i = 0; i < secret.length; i++) {
           var found = false;
           //if user finds right character, right spot
           if (userTry.charAt(i) === (secret.charAt(i))) {
             //if code and guess are a match, display letter in greeting
             li.innerHTML += userTry.charAt(i).fontcolor('green');
             found = true;
             //show user
           }
           //if character is in the code but in wrong
           else {
             for (var j = 0; j < secret.length; j++) {
               var found = true;
               if (userTry.charAt(j) === (secret.charAt(j))) {
                 li.innerHTML += userTry.charAt(j).fontcolor('red');
               }

               if (found === false) {
                 li.innerHTML += userTry.charAt(i).fontcolor('black');
                 //show user character is wrong

               }
             }
           }
         }
         ul.appendChild(li);
         alert(userTry);
       }

       window.onload = init;
     </script>
   </head>

   <body>
     <form>
       <input type="text" id="userGuess" size="15" placeholder="Your Guess">
       <input type="button" id="startButton" value="Place your Guess">
     </form>
     <ul id="guessList">
     </ul>
   </body>

   </html>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.