Warning messages for user

I am new to programming in javascript, I cant figure out how to make my code to prompt the user for at least 3 times, prompt, then alert, again prompt, then alert, again prompt then alert… once 3 times the user fails to type something then a final alert shows up like “No more attempts!” and the DIV turns grey color, could some someone help me?

Javascript:

var mObjt = {
    userInput: "",

    setInput: function(){
        document.getElementById("div1").innerHTML = mObjt.userInput;},

    conRequest: function(){
        if(mObjt.userInput != ""){
        mObjt.setInput();}
        else{
        alert("There is no input!");
        mObjt.popRequest();}},

    popRequest: function(){    
        mObjt.userInput = prompt("Enter a word:");
        mObjt.conRequest();}

HTML:

< div id="div1" style="width:200px; height:30px; border:1px solid;">< /div>
< button type="button" onClick="mObjt.popRequest()">Add Input</ button>

Hi,

Something like that? https://jsfiddle.net/0jkf4tu3/

[quote=“ultramann, post:1, topic:221777”]
I am new to programming in javascript, I cant figure out how to make my code to prompt the user for at least 3 times,[/quote]

Okay, so there are three attempts


var attempts = 3;

Which will require somewhere to save prompt answer

var answer = "";

We’ll keep on prompting

do {
    answer = prompt("Enter a word:");
} while (!answer);

We can update the while condition:

} while (!answer && attempts > 0);
if (!answer) {
    alert("No more attempts!");
}

Really?

CSS:

.disabled {
    background-color: gray;
}

JavaScript:

document.getElementById("div1").classList.add("disabled");

Done and done. :smiley:

var attempts = 3;
var answer = "";
do {
    answer = prompt("Enter a word");
    attempts -= 1;
} while (!answer && attempts > 0);
if (!answer) {
    alert("No more attempts!");
    document.getElementById("div1").classList.add("disabled");
}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.