-4

I have this code and it works pretty well. It gives me responses based off of who I put in the prompt. Only issue is that I want the code to give me a magic 8 ball effect when it doesn't recognize the name I put in. If I put in 'sdhjfgdsfdgds', for example, I want it to switch to the other code I have at the bottom of this question and give me a random response. If I put in 'Brandon', however, I still want it to tell him he's going to die. How do I accomplish this?

function myFunction() {
    var person = prompt("Whose future are you inquiring about?", "Brandon");  
    if (person === 'Brandon') {
        document.getElementById("demo").innerHTML =  "He will die a grusome death";
    }
    if (person === 'Daniel') {
        document.getElementById("demo").innerHTML = "He will receive free ice cream tomorrow";
    }
}
<p>Welcome.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

I have this code as well:

var future = ['bright', 'bleak', 'not good', 'will probably die a horrible death soon', 'ask him', 'happy', 'fulfilled']; var show = 
future[Math.floor(Math.random() * future.length)]; 
document.write(show);
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73

1 Answers1

1

What you want to do is use an if statement properly so what you will do is..

var futrue = [...]
var person = prompt("your text here").toLowerCase(); 
// the toLowerCase() will make sure that whatever they type will match what you have written
if(person === 'brandon') {
    do something
} else if (person === 'daniel') {
    do something
} else {
    var show = future[Math.floor(Math.random() * future.length)]; 
    document.write(show);
}
Smokey Dawson
  • 8,827
  • 19
  • 77
  • 152