So, I was just tinkering around with some javascript, trying to understand it a bit more. I noticed that there is the window.prompt()
script. It opens a little space where you can type in something, in the window. Then, there’s also alert()
.
We also have var
, for variables.
so, naturally, placing something as
var y = window.prompt("Some question");
alert("Something " + y);
would allow you to make a “responsive” answer. However, I noticed that if you closed the window.prompt, it still sends you to the alert, and the place where you added your variable will automatically have the world “null” added to it. I want to know if there is any way to stop that.
Something that could work so that if Close was clicked, then the alert wouldn’t appear.
<button onclick="hi()">
Hi
</button>
<style>
h1 {
color:red
}
button {
color:white;
background:black;
font-size:60px;
}
button:hover{
color:black;
background-color:white;
cursor:pointer;
}
</style>
<script>
function hi() {
var y = window.prompt("What are your favorite crackers?");
// I want to make it so that this doesn't show if the above window prompt wasn't answered. Currently, it only shows "null".
alert("Oooh! " + y + " are my favorite crackers, too!");
}
</script>