Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here\'s what I\'m trying to do 1 - create a javascript button that will bring up

ID: 3545993 • Letter: H

Question

Here's what I'm trying to do


1 - create a javascript button that will bring up a "login" prompt to enter a password.

2 - the "password" will be one word.

3 - no limit on password attempts, but if you get it wrong, the prompt changes to "incorrect - please try again"

4 - upon entering the correct password, the bottom of the existing page will read "password successful"


The password examples I've gone over (even in the book) are way more complicated than this.  There's got to be an easier way.


Thanks!

Explanation / Answer

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<input type="button" id="myButton" value="Click Me!" >

<p id = "success"></p>


<script>


var handler = function(){

document.getElementById("success").innerHTML = null;

var password = prompt("Please enter password: ");

var flag = true;

while(flag==true){

if(password=="password")

{

document.getElementById("success").innerHTML = "password successful";

flag=false;

}

else {

document.getElementById("success").innerHTML = null;

password = prompt("incorrect - please try again ");

}

}

}


document.getElementById("myButton").addEventListener("click" , handler);

</script>

</body>

</html>