Question
Help with my JavaScript Work. Add an else statement within the for loop to test whether the current character is in the code but not
Help with my JavaScript Work.
Add an else statement within the for loop to test whether the current character is in the code but not in the right location.
Build a second for loop (this will be in your else statement. Remember that your outer loop has already been created)
that iterates from 0 to secret.length
and checks the users guess character against all the other characters in the secret code
I have been trying for hours but I really can't figure out this step.
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
} else if((userTry.charAt(i))===(secret.charAt(0,1,2,3,4,5))){
li.innerHTML += userTry.charAt(i).fontcolor('red');
//this is the end of 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