Javascript comparisons not working with prompts, while loop, and !== - javascript

What I'm trying to do is prompt the user repeatedly until one of the accepted answers is received. Pretty easy stuff. The while loop, however, is making this really weird and annoying. Here's what I got:
var plrchoice=prompt("Would you like to choose Bulbasaur, Charmander, or Squirtle? (Use only lowercase characters)");
while(plrchoice!=="bulbasaur"||plrchoice!=="charmander"||plrchoice!=="squirtle"){
plrchoice=prompt("Would you like to choose Bulbasaur, Charmander, or Squirtle?");
}
This should work in theory, but the result is an infinite do/while, regardless of what the user inputs. Thanks in advance :)

Those || should be &&.

If your rewrite the code like this, it may be clearer what happens:
var plrchoice = "";
while (!/^(bulbasaur|charmander|squirtle)$/i.test(plrchoice)) {
plrchoice =
prompt("Would you like to choose Bulbasaur, Charmander, or Squirtle?");
}

Related

How to use JavaScript in Node-red to display message based on dropdown options

Given this Node-red flow:
I am unfortunately unsure about how I should implement it in function.
I have 3 options in both dropdowns, one displaying meattype the other doneness. Whenever I choose a combination of the dropdowns, I should have the proper temperature given in the text output.
I have tried using If, but I still have problems with getting it to work.
I only have very basic knowledge of Javascript language, so I hope someone could help and at least lead me in the right direction.
Thank you
EDIT:
In the If code in my function node I had "A conditional expression" present, though I got this fixed by changing If from:
if(msg.payload = "")
to
if(msg.payload == "")
This fixed my code and gave me the expected results.
var payload = msg.payload
if(msg.payload == "KalvRoed"){
msg.temperature = "53-57 grader"}
I had "A conditional expression" which means I had to change my code, so that "if" was written with "==" rather than "=". If "if" had been written with "=" the function node would think that I was creating a variable, while I was trying to have it do something if was true.
var payload = msg.payload
if(msg.payload == "KalvRoed"){
msg.temperature = "53-57 grader"}
Hope that makes sense

Problem with a do-while loop and creating an alert

I'm new to javascript and I'm trying to make a program to continuosly click one button unless another button is present. (I'd also love to get an alert when the second button appears but I don't know how to do that.)
This is what I got:
Do {Let button=document.getElementById("find");
Let want= document.getElementById("bba");
setInterval(function(){
button.click();
}, 10000); }
while (want.click=false)
I keep getting errors (unidentified syntex). I'm not sure how to fix it.
Any help will be greatly appreciated!
What you should do is use a single setInterval :
window.setInterval(function() {
if (!document.getElementById("button2")) {
document.getElementById("button1").click();
} else {
document.getElementById("button2").click();
alert("second button appeared");
}
}, 100);
Sorry for formatting, I'm on mobile.
Just from looking over your code I see two main errors. The first is that you used a capital do and let, JavaScript is case sensitive so you need to use lowercase. The second is that in you wrote
while (want.click=false)
What you wrote is an assignment not a equality check.
while (want.click == false)
That's the correct way to write it.
Your do and let keywords are capitalized. They should be lower case.
Let want= document.`getElementById`("bba");
You should use a triple equal sign here rather than an assignment operator.
it should be: let want === document.getElementById("bba");

prompt number that scrolls with loop using if and else

I'm trying to insert a number in a prompt and scroll the screen using a for loop with if and else statements, and not success. The code behaves in way that when the loop iterates, the alert seems to pop in the process, and not as a result in the end "a pop up" saying there is no such number in the database to reach for, or the final result if there is the right number according to what was in the range(of numbers possible into entry) and without the pop up iterating, neither a pop up popping at least once when not wanted. The way I want the code to behave is, if there is no matching number then the alert should pop up. I would accept even a array in exchange of a better answer to the code I mentioned bellow, so that I am not restricted to a few tools(sort of say) and everything is welcome for the learning purpose. Though if there is a better solution but can be done with/without the tools I mentioned, I would pick the few tools(codes) I mentioned and it is what the preferable answer and just in case there is solution with it(the few codes I mentioned) or in case if not, I'm open to more tools available.
This is not even my best shot:
function off() {
var ik = prompt("type a number from 1 to 1000");
for(var i=0;i<1000;i++){
if(ik == i){
window.scrollTo(1366*i, 0);
break;
} else {
alert("");
}
}
}

My java script code makes all of the other codes stop working. If, Else loops

So I am trying to make a game where you choose your own adventure. But the loop I made with prompt does not work and makes the other code not work. I am new a java script so I am still trying to figure everything out. If anyone is able to help, I would appreciate it. The code I am using is
function myFunction() {
var option = prompt("Please enter Bat or Metal");
if (option == "bat" or "Bat" ) {
document.getElementById("path1c").innerHTML =
"Congrats! You killed the creature, but got bitten by the creature.";
else {
document.getElementById("path1c").innerHTML =
"Congrats! You killed the creature and were not harmed.";
}
}
Also I am using a button that calls myFunction on click, so that a prompt will show up.
JS doesn't have or keyword, should be ||:
if (option == "bat" || option == "Bat" ) {
Syntax wrong on if/else, brackets must match. You missed a } before else, should be:
if(/*condition*/) {
// something here
} else {
// something here
}
No offense, but judging from your code, especially the usage of or, you're just guessing the language and bidding your luck. You won't go long this way. What you really need to do now is to grab a fundamental book and read.
EDIT:
JavaScript and Java are related indeed. When Brendan Eich designed the language, there was an order from the company high level that said, make it look like Java. There are many evidances that could proove it, explicitly or implicitly. For example, in many places Brendan admitted that Date in JavaScript was manully ported from java.util.Date (like this answer on Quora).

Javascript Numeric Form Validation

I made a simple little calculator for math class. It does simple things like find volumes and areas of certain shapes, but saves me a lot of time in homework.
I uploaded it to the internet for my classmates, but I figured I would make the forms only be able to have numbers in them. I found some answers on tizag, but I don't really understand those solutions.
I'm looking for something like this:
function calculation() {
if (form.thenumbers.value = code to check if it is numeric)
{
calculations
} else {
alert("Numbers only please");
}
}
If it can't be that simple, I just appreciate a little explanation to how any other way works. Thanks.
if (form.thenumbers.value.match(/^[\d]*$/)){
//do stuff
}
Should work for you. This will match the value for a 0 or more digits. If you would like it to match for 1 or more, use + in place of *.

Categories