/    Sign up×
Community /Pin to ProfileBookmark

Need help with counting code

I have a password program where you get 3 tries to enter the password. This part works great.

Now I want to add a 2 features that gives you the number of tries you have left and then when you get it right the message box tells you that you got it right in x number of tries.

I imagine I could use a for loop to count but am not sure where to place it? Would I need 2 loops, one to increment and one to decrement number of tries?

Thanks.

MoJo

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisMar 06.2003 — You obviously already have a variable that contains the number of tries. Therefore, you don't need a "for loop". All you have to do is display that variable after each try.

Or, since you haven't shown any code to tell what you are doing, you can try this:<script>
var pw = "homer"
var msg1 = "Please enter password: ";
var msg2 = "You have ";
var msg3 = " tries left!";
var msg3a = " try left!";
var msg4 = "You go it in ";
var msg5 = " tries!";
var msg5a = " try!";
var msg6 = "You blew it!";
var success = false;
for (var i=1; i<=3; i++) {
var guess = prompt(msg1, "");
success = (guess == pw);
if (success)
{break;}
else
{if (i != 3)
{st = msg2 + (3-i);
st += (3-i == 1) ? msg3a : msg3;
alert(st);}
else break;
}
}
if (success)
{st = msg4 + i;
st += (i == 1) ? msg5a : msg5;
alert(st);}
else
{alert(msg6);}
</script>
Copy linkTweet thisAlerts:
@MoJoauthorMar 07.2003 — Thanks gil! Here is the code I am working on. You can see that the code will just allow three tries for the password so this works. I created the numTries variable for the number of tries but not sure what to do. I want the number of tries to be in the user prompt if they don't get it right like "That's not right. You have X tries left."

Then when they get it right the alert message should say, "You got it right in X number of tries."

Thanks for the help!



< <script>

function main(){


//create a string variable called userResponse
//Purpose: string variable that holds what the user guesses
var userResponse = ""; // string string variable that holds what the user guesses

//create a string variable called password
//Purpose: string variable that holds the correct password
var password = "BLIZZARD"; // string string variable that holds the correct password

//create a integer variable called counter
//Purpose: integer variable for the counter
var counter = 0; // integer integer variable for the counter

//create a integer variable called numTries
//Purpose: integer variable that holds the maximun number of tries
var numTries = 3; // integer integer variable that holds the maximun number of tries

//ask user '"What is the secret password? HINT: It is BLIZZARD!"'
//and put the answer in userResponse
userResponse = prompt("What is the secret password? HINT: It is BLIZZARD!");

// if (userResponse.toUpperCase() != password)is TRUE, then:
if (userResponse.toUpperCase() != password){

<i> </i>//count from 2 to 3 using counter
<i> </i>for (counter = 2;counter&lt;=3;counter++){

<i> </i> //ask user 'Sorry you're wrong%n What is the secret password? %n HINT: Is is BLIZZARD!'
<i> </i> //and put the answer in userResponse
<i> </i> userResponse = prompt("Sorry you're wrong%n What is the secret password? %n HINT: Is is BLIZZARD!");

<i> </i> // if (userResponse.toUpperCase() == password)is TRUE, then:
<i> </i> if (userResponse.toUpperCase() == password){

<i> </i> //replace value of counter with 4
<i> </i>counter=4;

} // end if

} // end for

} // end if

// if (userResponse.toUpperCase() == password)is TRUE, then:
if (userResponse.toUpperCase() == password){

//Tell user: "You got it right!"
alert("You got it right!");

//otherwise...
}else{

//Tell user: "You are out of tries!"
alert("You are out of tries!");

} // end if

} // end main

<i> </i>&lt;/script&gt;
Copy linkTweet thisAlerts:
@MoJoauthorMar 08.2003 — Nevermind on the help. I figured it out myself. ?

MoJo
×

Success!

Help @MoJo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...