every keypress for prompt-sync in javascript repeats the prompt - javascript

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.

Related

If and else function didn't work on javascript

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');
}

whats wrong with this JS? should be quick fix

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!");
}
};

JS Prototype not running through its full function

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");
}
})
})

javascript which I wrote is wrong(may be)

I have tried to write js for my html form. js is working fine with the logically. But if logic fails,I mean if any condition fails it reloads the page,which I don't want. I am providing the code. Please point me out the mistake in js if any.
window.onload = function() {
document.getElementById('submitlink').onclick = function() {
var bflag = document.addpro.brandflag;
var brand = document.addpro.brand1.value;
var cflag = document.addpro.catflag;
var cat = document.addpro.cat1.value;
var color1 = document.addpro.color1.value;
var color2 = document.addpro.color2.value;
if(cb_validation(bflag,brand))
{
if(cb_validation(cflag,cat))
{
if(colorcheck(color1,color2))
{
document.getElementById('addproform1').submit();
return false;
}
}
}
}
function cb_validation(flag,field)
{
if(flag[0].checked)
{
if(field==0)
{
alert('Please Select Both Brand And Category');
field.focus();
return false;
}
else
return true;
}
else
return true;
}
function colorcheck(c1,c2)
{
if((c1==0) && (c2==0))
{
alert('Please Select Both Colours');
document.addpro.color1.focus();
return false;
}
else if((c1==0))
{
alert('Please Select 1st Colour');
document.addpro.color1.focus();
return false;
}
else if((c2==0))
{
alert('Please Select 2nd Colour');
document.addpro.color2.focus();
return false;
}
else
return true;
}
}
I am rookie in js. Please also tell me if I have done any mistake.
return false is what keeps the page from reloading. Right now it is inside your final color check condition. If you never want the page to reload it needs to be after your first cb_validation condition.
Submit() is causing the page refresh which is in below line
document.getElementById('addproform1').submit();
Also both your function is returning true becauseyou are returning true in else block. Hope this points you to right direction....
Good luck....

jquery form validation without click -> when ok show div

is it possible to do this automatically. mean when i type text and click on the second textfield autocheck the first one. then when both ok show the div2 and so on.
here is some code
var step1 = function() {
var first = $("#f_name").val();
var last = $("#l_name").val();
var error = false;
if (first == "") {
$("#f_name").next().text("*ErrorMsg");
error = true;
} else {
$("#f_name").next().text("");
}
if (last == "") {
$("#l_name").next().text("*ErrorMsg");
error = true;
} else {
$("#l_name").next().text("");
}
if (error == false) {
$("#send").submit();
$('#div1').show('slow');
} else {
returnfalse;
}
}
var step2 = function() {
var email1 = $("#e_mail").val();
var adress1 = $("#adress").val();
var error2 = false;
if (email1 == "") {
$("#e_mail").next().text("*ErrorMsg");
error2 = true;
} else {
$("#e_mail").next().text("");
}
if (adress1 == "") {
$("#adress").next().text("*ErrorMsg");
error2 = true;
} else {
$("#adress").next().text("");
}
if (error2 == false) {
$("#send2").submit();
$('#div2').show('slow');
} else {
returnfalse;
}
}
$(document).ready(function() {
$('#div1').hide();
$('#div2').hide();
$("#send").click(step1);
$("#send2").click(step2);
});
hope anyone can help me. and sorry for my bad english :)
greatings
The way that I would do it is:
Assign a variable, something like numSteps and set its initial value to 1
onFocus and onBlur, run a function that steps through each field, based on numSteps
If any fields are empty (or however you want to validate them), set error = true
if !error numSteps++
Make all elements up to numSteps visible
Hope this helps
Very crude example, but demonstrates what I was referring to:
http://jsfiddle.net/aSRaN/

Categories

Resources