Just not sure why this doesn't work? the prompt work but nothing after.
function btn(){
prompt('do you like banana?');
if("yes"){
alert = "good";
}
else {
alert = "to bad, try again!";
}
};
Just condensing Sabar's answer and changing alert = ... to the method version (which I'm guessing is what you wanted), it could also be written as:
With a prompt(), which would return what the user enters (random is possible)
function btn(){
if(prompt('do you like banana?') == "yes"){
alert("good");
} else {
alert("to bad, try again!");
}
};
Or with a confirm()
function btn(){
if(confirm('Would you like banana?')){
alert("good");
} else {
alert("to bad, try again!");
}
};
You could take it even further if you like with:
function btn(){
confirm('Would you like banana?') ? alert("good") : alert("to bad, try again!");
};
Or even one step more into the realm of harder to read (as suggested by nnnnnn)
function btn(){
alert(confirm('Would you like banana?') ? "good" : "to bad, try again!");
};
prompt is a function which returns what the user typed, so
function btn(){
userAnswer = prompt('do you like banana?');
if(userAnswer === "yes"){
alert("good");
}
else {
alert("to bad, try again!");
}
};
this should work.
Also, alert is a function, and by writing alert = "asdad", you are instead making it just a string. Also, if("yes") just checks if the string "yes" is evaluated to true or false, and it is always evaluated to false. You may want to take a look at some online course for Javascript, for example this
Are you sure what you want is a prompt?
From your code i understand that it's a confirm.
function btn() {
var r = window.confirm("do you like banana?");
if (r == true) {
alert("good");
} else {
alert("to bad, try again!");
}
};
If you want a prompt then use this:
function btn() {
var r = prompt("do you like banana?", "I'm a monkey!!!");
if (r != null) {
alert("good. " + r);//good. I'm a monkey!!!
}
else
alert("to bad, try again!");
};
Prompt returns null if you have cancelled it, and it select value from textbox if you select ok.
function btn(){
var response = prompt('do you like banana?',"yes");
if(response){
alert("good");
}
else {
alert("too bad, try again!");
}
};
You have to save the value of prompt to a variable:
function btn(){
var response = prompt('do you like banana?');
if(response == "yes"){
alert("good");
}
else {
alert("too bad, try again!");
}
};
Related
Code:
var prompt = require("prompt-sync")()
function start() {
var whatbox = prompt('What box would you like to open?(ALL LOWER CASE)')
if (whatbox == "School") {
box("School")
} else if (whatbox == "Park") {
box("Park")
} else if (whatbox == "Tech"
} {
box("Tech")
} else {
console.log("That is NOT a box! Try again and check your spelling!")
}
}
start()
Error:
The prompt keeps on repeating every time I press a key.
var prompt = require("prompt-sync")()
function start() {
console.log("What box would you like to open?(ALL LOWER CASE)")
var whatbox = prompt("Your input >> ")
if (whatbox == "School") {
box("School")
} else if (whatbox == "Park") {
box("Park")
} else if (whatbox == "Tech"
} {
box("Tech")
} else {
console.log("That is NOT a box! Try again and check your spelling!")
}
}
start()
The prompt-sync has this problem where if your prompt text/qustion is too long it fails when refreshing it. To solve this problem, you can simply console.log question and then ask for input.
I'm new to here. So I was just making a simple code just for fun, but then I was confused why the if and else function can't work. Can anybody help me?
var name = prompt("Please enter your name","Your name");
if(name != null) {
alert("Hello, " + name + "!");
}
// Google and Youtube redirect
if (name.onclick == true); {
confirm("If you like to go to google, Click OK. If you like to go to youtube, Click CANCEL")
};
if ( confirm == true ); {
window.location.replace("https://www.google.com/")
} else {
window.location.replace("https://www.youtube.com/")
};
You need to assign whatever confirm returns to a variable, and check that.
let answer = confirm("If you like Apples, click OK. If you like Bananas more, Click CANCEL");
if (answer === true) {
console.log('you like apples');
} else {
console.log('you like bananas');
}
This is my code, I'm attempting to set up a combo lock for a game that will unlock when the user inputs the correct answer. However, when the user inputs something it will always return "The door is unlocked." What am I missing or doing wrong here?
function comboLock(){
this.comboLock = ["666"];
this.falseInput = /[^1-5, 7-9]/g;
}
comboLock.prototype.unlock = function(){
if (this.comboLock != this.falseInput){
return alert("the door is unlocked");
}
else if (this.comboLock === this.falseInput){
return alert("that is not the correct combonation");
}
}
$(document).ready(function(){
$("#button-unlock").click(function(event) {
event.preventDefault();
if (this.comboLock = "666"){
return alert("the door is unlocked");
}
else if (this.comboLock != "666"){
return alert("that is not the correct combonation");
}
})
})
I have a javascript function that calls my javascript function, my func islike this:
function sugParitValidation(){
var isValid = false;
Ext.MessageBox.confirm(' ','Are you sure you want to do this?', function(btn){
if(btn == 'yes'){
isValid = true;
}
});
return isValid ;
}
My problem is - if statement and the return statment is happening, and only after that the confirm window being shown.
That way I can't react to what the user choosed.
how to solve this? tried allready use setTimeOut, no change at all....
i think you are trying to do something like this.
someFunction(){
if(sugParitValidation()){
//todo something
}
else{
//todo another thing
}
}
you can do this easyly with callbacks. This is the example.
someFunction(){
var messageCallback = function(btn){
if(btn === 'yes'){
//todo something
}
else{
//todo another thing
}
}
Ext.MessageBox.confirm(' ','Are you sure you want to do this?',
messageCallback);
}
I want to check a textarea whether it is empty or not. For this I write the following code:
function validateForm(theForm) {
var problem_desc = document.getElementById("problem_desc");
if (problem_desc.value == '') {
alert("Please Write Problem Description");
return false;
} else {
return true;
}
}
For the first time it is working fine. But If I remove the text from the textarea the above function is returning true value i.e., it is assuming the previous text I've entered into the textbox. Can anybody kindly tell me where is the problem?
I am getting it correctly. This is what I did.
Click on validate, it said Please Write Problem Description.
Write something and click. Nothing happened.
Remove the text. Click on validate, it said Please Write Problem Description.
Note: Use a trim function to eliminate empty spaces.
Code:
function validateForm(theForm) {
var problem_desc = document.getElementById("problem_desc");
if ($.trim(problem_desc.value) == '') {
alert("Please Write Problem Description");
return false;
} else {
return true;
}
}
Demo: http://jsfiddle.net/TZGPM/1/ (Checks for Whitespaces too!)
Do check for white space in the value like this
if (problem_desc.value.match (/\S/)) { ... }
or other way check for length
problem_desc.value.length == 0;
Remove spaces and calculate length of the value attribute.
function validateForm(theForm) {
var problem_desc = document.getElementById("problem_desc");
if (problem_desc.value.replace(/ /g,'').length) {
return true;
} else {
alert("Please Write Problem Description");
return false;
}
}
<textarea id="problem_desc"></textarea>
<button onclick="validateForm()">Validate</button>