Why does this if statement not recognise numbers? [duplicate] - javascript

This question already has answers here:
How to get numeric value from a prompt box? [duplicate]
(6 answers)
Closed 2 months ago.
This code works fine on letters, returning if the letters are upper or lower case but it always returns the number as "6 is a uppercaseletter"
let userLetter = prompt("Please enter an uppercase, lowercase letter or a number");
if (typeof userLetter === 'number') {
userLetter = userNumber;
alert(`${userNumber} is a number`);
} else if (userLetter.toUpperCase() === userLetter) {
alert(`${userLetter} is a uppercase letter`);
} else if (userLetter.toLowerCase() === userLetter) {
alert(`${userLetter} is a lowercase letter`);
} else {
alert('Something went wrong');
}
I expected the typeof operator would recognise that the input is a number but it doesn't. When a number is entered it always returns the uppercase option.

prompt always returns a string (for which typeof returns "string").
You could use isNaN instead.
if (!isNaN(userLetter)) alert(`${userLetter} is a number`);

Related

How can I get my code to check if I entered in an integer? [duplicate]

This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Validate that a string is a positive integer
(16 answers)
Closed 2 years ago.
My code isn't working on my JavaScript project. I want it to check if you inputted an integer or not, and if you did, check if you inputted a whole number. I expected it to accept the number "3" in my prompt and say it was an integer. Instead, everything I put in "isn't an integer." Can someone help? Here's a copy of my code:
function changeTime() {
var changeTimee = prompt("Enter in a new amount of seconds. Leave blank or press cancel to cancel.");
console.log("You entered: " + changeTimee);
if (Number.isInteger(changeTimee)) {
if (floor(changeTimee) === changeTimee) {
if (changeTimee === "" || changeTimee === null) {
console.log("Left Blank");
} else {
seconds = changeTimee;
}
}
} else {
alert("Please enter positive integer, not a string.");
}
console.log(seconds);
}

Match function is giving wrong results [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 4 years ago.
I'm trying to do match a phone number with a regular expression:
this.Formareamedia.get('ladacontacto').valueChanges.subscribe((lada) => {
let p;
if (lada.length == 5) {
p = '\\d{3}.\\d{4}';
} else {
p = '\\d{4}.\\d{4}';
}
this.Formareamedia.get("telefonocontacto").setValidators(Validators.pattern(new RegExp(p)));
this.Formareamedia.get("telefonocontacto").updateValueAndValidity();
this.ladacontacto = lada;
let telefono = this.Formareamedia.get('telefonocontacto').value;
console.log(new RegExp(p).lastIndex);
if (telefono && telefono.match(new RegExp(p))) {
return null;
} else {
return {
telefono: false
}
}
});
If I put in the lada input (XX) and in the telefono input XXXX-XXXX the function is returning true (a correct result), but if I put in the lada input (XXX) and in the telefono input XXXX-XXXX is returning true a wrong result it is supposed to return false. What's wrong with my function?
You need to anchor your regex to the end of the string, like this:
if (lada.length == 5) {
p = '^\\d{3}.\\d{4}$';
} else {
p = '^\\d{4}.\\d{4}$';
}
Otherwise it will match at least the number you specify (ignoring extra digits).
The same applies to the beginning of string: Specify '^'.
With '$' at the end it will ensure that the string ends with your digits.

Why is my JS function always returning true? [duplicate]

This question already has answers here:
Why doesn't my simple if-statement render false in javascript?
(2 answers)
Closed 6 years ago.
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9).
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long. Why?
= is always assignment. Equality comparison is == (loose, coerces types to try to make a match) or === (no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '') is that the assignment str = '' is done, and then the resulting value ('') is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since '' is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9) step. Since at that point, str.length is 0, that's the path the code takes.

Why doesn't my equality comparison using = (a single equals) work correctly? [duplicate]

This question already has answers here:
Why doesn't my simple if-statement render false in javascript?
(2 answers)
Closed 6 years ago.
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9).
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long. Why?
= is always assignment. Equality comparison is == (loose, coerces types to try to make a match) or === (no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '') is that the assignment str = '' is done, and then the resulting value ('') is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since '' is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9) step. Since at that point, str.length is 0, that's the path the code takes.

How to check value in javascript variable for include number or not? [duplicate]

This question already has answers here:
Check whether an input string contains a number in javascript
(15 answers)
Closed 7 years ago.
How to check value in javascript variable for include number or not ?
eg: var test = "abcd1234";
this var test include lower case and number.
How to use javascript to check var value include number or not ?
I tried to use isNaN function
var test = "abcd1234";
var test = isNaN(test);
if(test === true)
{ alert("not include number");
else
{ alert("include number");
But this code alert not include number because isNaN will check all data of var. but i want to check only a part of var. How can i do that ?
You must use regex instead.
var test = /\d/.test("abcd1234");
if (test === true) {
alert("include number");
} else {
alert("not include number");
}

Categories