简体   繁体   中英

How do I take in the value of this prompt and put it into the if/else statement

I am trying to ask for a username in the prompt stored in "name" var, if the user entered nothing, it would randomly select a value from the array stored in randomName array. But it is returning neither the entered value or anything from the array, all it returns is "Your name is". Why?

This is what have so far

var name=prompt("Please enter a usename");
if(name===" "){
    var randomName=["Spill Cramer","Oberon Gorgeous","Pointy Binge","Lord Inquiry","Lower Salmon","Auk Cheeks","Minty Lub","Sonore Dim","African Hernia","Squares Pine","Promise Waterpolo","Cucumber Wizard","Tactful Rampallian","Mars Pawn","Usually Dispatch","Close Baffled","Adaptive Mugger","Involved Neuron","Position Stash","Buttercream Oxidant","Bus Hootenany","Small PHP","Copernicium Hematoma","Nostalgic Relay","Wetsuit Swinhoe"]
    var rand=randomName[Math.floor(Math.random()*randomName.length)];
    if(rand===0){
        alert("Your name is Overfilled Lemon");
    }
    else{
        alert("Your name is "+rand)
    }
}
else{
    alert("Your name is ",name);
};

The problem is if(name===" ") will only enter its code block if the user enters a space into the prompt. Change it to be if(name===""){ and it should work properly.

There were some minor issues. Below is working code. Check this fiddle. https://jsfiddle.net/n6oqx441/

var name=prompt("Please enter a usename");
console.log(name)
if(name=== ''){
    var randomName=["Spill Cramer","Oberon Gorgeous","Pointy Binge","Lord Inquiry","Lower Salmon","Auk Cheeks","Minty Lub","Sonore Dim","African Hernia","Squares Pine","Promise Waterpolo","Cucumber Wizard","Tactful Rampallian","Mars Pawn","Usually Dispatch","Close Baffled","Adaptive Mugger","Involved Neuron","Position Stash","Buttercream Oxidant","Bus Hootenany","Small PHP","Copernicium Hematoma","Nostalgic Relay","Wetsuit Swinhoe"]
    var rand=randomName[Math.floor(Math.random()*randomName.length)];
    if(rand===0){
        alert("Your name is Overfilled Lemon");
    }
    else{
        alert("Your name is "+rand)
    }
}
else{
    alert("Your name is "+ name);
};

Speaking from a UX perspective, you probably don't want to be using prompt as a way to collect input, Maybe you want to look at BootStrap for a nicer way to collect input.

The reason you might be getting the error is that if the user enters nothing, the result of the prompt is actually null.

You may instead want to do something like this:

var name = prompt("enter name: ").trim(); 
if(!name || name === ""){
 //select random name
}

Using trim will remove leading and trailing whitespace characters making it much easier for comparisons, meaning that if the user where to only enter spaces, the would be assigned a random name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM