Question
Help With JavaScript Homework! I can't figure this out, I've been trying for hours. Instructions: 1Add an else statement within the for loop to test
Help With JavaScript Homework! I can't figure this out, I've been trying for hours.
Instructions:
1Add an else statement within the for loop to test whether the current character is in the code but not in the right location.
2Build a second for loop (this will be in your else statement. Remember that your outer loop has already been created)
2a.that iterates from 0 to secret.length
2b.and checks the users guess character against all the other characters in the secret code
3.If a match is found
3a.set found to true
3b.add the character to the innerHTML of the li with the fontcolor set to red
4.Test that the guess now outputs each time with:
4a.green letters for an exact match
4b.red letters for a correct letter in the wrong location
4c.black letters for a letter not in the secret code
My Code:
var secret = 'ABCDEF';
function init(){
var button = document.getElementById('startButton');
button.onclick=myButtonClick;
}
function myButtonClick() {
//alert('Button clicked');
// var userAttempt = document.getElementById('userGuess')
// var userTry = userAttempt.value;
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 ((userTry.charAt(i))===(secret.charAt(i))) {
//if our code and guess are a match, display letter in green
li.innerHTML += userTry.charAt(i).fontcolor('green');
found = true;
//need to show the user
//this is my part
}
//if character is in the code but in the wrong place
if (found == false) {
li.innerHTML += userTry.charAt(i).fontcolor('black');
//need to show user character is totally wrong
}
}
ul.appendChild(li)
alert(userTry);
}
window.onload = init;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started