JavaScript if statements not functioning [duplicate] - javascript

This question already has answers here:
What is the correct way to check for string equality in JavaScript?
(11 answers)
Closed 4 years ago.
I'm sure there is a perfectly logical explanation to why this isn't working the way I want it to but I am new to JavaScript so would love some help. Is there any reason you would know as to why it prints out yes even when I want it to print out no.
Thanks in advance
<script type="text/javascript">
var username = prompt("What is your VC?");
if (username = "wow") {
greeting = document.write("yes");
} else {
document.write("no");
}
</script>

The = operator is used for assignment, for checking the value you can use ===.
So, change the if as follows:
if (username === "wow")
Working code is given below:
var username = prompt("What is your VC?");
if (username === "wow") {
greeting = document.write("yes");
} else
{
document.write("no");
}

Yes. One = is assignment. Two == tests equality. And three === tests strict equality.
var username = prompt("What is your VC?");
if (username === "wow") {
document.write("yes");
} else {
document.write("no");
}

When you say:
a = b
You're saying "set a to the value in b."
If you out that in an if statement's expression, it assigns b to a and thrn checks if a's new value is "truthy."
If you want to ask "is a equal to b," you have to say:
a == b
I know that there is a difference between == and ===, but I am not a JavaScript master, so I cannot tell you what the difference is!

Related

if statement not working when i try to use it in my code to check a variable

i am trying to detect what the variable is but it is not working
var question = prompt("press 1 for hi logged in console and press anything else for goodbye logged in console")
if (question ===1
) {
console.log("hi")
}else{
console.log("goodbye")
}
That's how == and === are different. You compare by === and it compares both value and type. prompt always returns string and '1' is not same as 1. So you either need to use == or compare as question === '1'.
prompt returns a string, you care compare the return value question to the value 1 as string as follows:
var question = prompt("press 1 for hi logged in console and press anything else for goodbye logged in console")
if (question === "1"
) {
console.log("hi")
}else{
console.log("goodbye")
}

Accepting an UpperCase value in Javascript [duplicate]

This question already has answers here:
How to do case insensitive string comparison?
(23 answers)
Closed 2 years ago.
I understand we need to use toUpperCase, but I wasn't sure where to put it. I would like the word "toyota" to be acceptable whether it's uppercase or lowercase.
let correctGuess = false;
let car = "toyota";
let guess = prompt ('guess the car');
if (guess === car){
correctGuess = true;
}
if (correctGuess === true){
console.log ('correct');
}
else {
console.log('incorrect')
}
Convert both of them to either uppercase or lowercase and compare.
Example:
if(guess.toLowerCase() === car.toLowerCase()) {
// your logic
}
Although you have already defined car as lowercase so you don't really need to convert it to lowercase.
Change
if (guess === car){
correctGuess = true;
}
To
if (guess.toUpperCase() === car.toUpperCase()){
correctGuess = true;
}
Kind of redundant but it gets useful if the car variable itself was taken from a user input.
You can also make both .toLowerCase to get the same result, literally no difference

What does the "?" operator do in Javascript? [duplicate]

This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 3 years ago.
I am wondering what this question mark symbol means in a function return statement in JS.
function getValue(val) {
return (val != null ? val.toString().replace(/,/g, '') : "");
}
Its a Conditional (Ternary) Operator:
Syntax:
variablename = (condition) ? value1:value2
Example:
var voteable = (age < 18) ? "Too young":"Old enough";
Explanation:
If the variable age is a value below 18, the value of the variable voteable will be "Too young", otherwise the value of voteable will be "Old enough".
In this case "?" allow to write if ... else in one line, it's what we called ternary operator, refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
It's a way to choose a value conditionally based on another value.
Variables are 'truthy' in javascript, and so let's say you have a variable x, and you want to choose variable y based on if variable x is truthy or not
var y = x ? '1' : '2';
If x is truthy, y will be '1', otherwise '2'.

How to validate whether a variable is valid integer allowing +/-? in pure javascript [duplicate]

This question already has answers here:
How to check if a variable is an integer in JavaScript?
(41 answers)
Closed 3 years ago.
I need to validate whether a input is a valid integer allowing for +/- entries.
I have tried this.
function valid(elem)
{
var num = parseInt(elem);
if(!Number.isInteger(num))
{
alert ("Not an integer");
}
}
But the issue here is , it is validating even strings like 10sd as an integer. So how to validate this?
I want to validate as following:
valid(-10) = true;
valid(+10) = true;
valid(-10.01) = false;
valid(10sd) = false;
valid(10.23see) = false;
valid(10) = true;
Simple
function valid(elem){return parseInt(elem)==elem}
function valid(value) {
if(typeof value === 'number' && isFinite(value))
alert ("It is an integer");
}
This function does the job, typeof is a keyword in Javascript which tells you the data type of the variable
You can check this out, for more usage of typeof:
https://webbjocke.com/javascript-check-data-types/
Edit: made a silly error in alert, it should alert if it is an integer, the if condition checks for number

How do i check for a specific value in a field using javascript?

The javascript checks form values for not being empty
if(!$('nradulti').value){ok=2;}
if(!$('transport').value){ok=2;}
if(!$('plata').value){ok=2;}
if(ok==1){return true;}
else if(ok==2){
$('mesaj').innerHTML="
<span class='notarea'>Please complete all fields.</span>";
return false;
}
I need to add a new variable called spam for captcha and check that it is equal to a certain number, e.g. "what's 2+2" and the script to proceed only if the number is 4
What line do i need to add please?
Thanks!
You can accomplish this with a basic equality check. I'm not exactly sure what framework your using, but given your other calls.
if(!$('spam').value == '4'){
ok=2;
}
Add this line to your js file and declare the value for spam before this.
if($('spamInput_field_selector').value!=spam){ok=2}
Try this way
var answer = document.getElementById("answer").value;
var digit1 = parseInt(document.getElementById("digit1").innerHTML);
var digit2 = parseInt(document.getElementById("digit2").innerHTML);
var spam = digit1 + digit2;
if(answer == ""){
alert("Please add the numbers");
}else if(spam==answer){
// call script //
}

Categories

Resources