If statement not executing when credentials are met [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to make a simple code that checks if the phone number entered by the user is in the proper format, but my if statement will not execute when all three credentials are met, it will only print the "please type the phone number..." or a blank screen, even if I type in the number correctly.
var phonenumber=prompt("What is your phone number?", "");
var firstdash=phonenumber.charAt(3);
var seconddash=phonenumber.charAt(7);
var length=phonenumber.length;
if(firstdash=='-' && secondash=='-' && length==12)
{document.write("Your phone number is "+phonenumber+", thank you.");}
else
{document.write("Please type the phone number in the proper format (555-555-1234)");}

seconddash is spelled wrong in your condition, you're missing the second d - always have your console open when developing.. you would have seen:
Uncaught ReferenceError: secondash is not defined

It should be seconddash not secondash in the if statement.
var phonenumber=prompt("What is your phone number?", "");
var firstdash=phonenumber.charAt(3);
var seconddash=phonenumber.charAt(7);
var length=phonenumber.length;
if(firstdash=='-' && seconddash=='-' && length==12)
{document.write("Your phone number is "+phonenumber+", thank you.");}
else
{document.write("Please type the phone number in the proper format (555-555-1234)");}

Can I recommend a different approach? If you are only checking the number is in the right format, then use a regex:
var phoneNumber = prompt("What is your phone number?", "");
if(isValidPhoneNumber(phoneNumber)) {
document.write("Your phone number is "+ phoneNumber +", thank you.");
} else {
document.write("Please type the phone number in the proper format (555-555-1234)");
}
function isValidPhoneNumber(phoneNumber)
{
return /^\d{3}-\d{3}-\d{4}$/.test(phoneNumber);
}

Related

I don't know endsWith in javascript work like this and more1111 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
const str = 'Do. Or do not. There is no try.';
console.log(str.endsWith('try', 30)); //true
console.log(str.endsWith('try.', 30)); //false
Can anyone explain why in example 2 it is false. I think it is similar with above and must return true?
Because in endsWith() the second parameter is length.
The string
Do. Or do not. There is no try.
is 31 characters long, by setting the length parameter to 30, you're only testing
Do. Or do not. There is no try
which does not end with try.
As Ian said, you can omit the length parameter to test the entire string:
const str = 'Do. Or do not. There is no try.';
console.log(str.endsWith('try')); // false
console.log(str.endsWith('try.')); // true
It's because y is the 30th character, and in the length parameter of the function call you've specified it should test the ending of the first 30 characters of the string. Therefore it doesn't take the . (which is the 31st character) into account.
You're actually only testing the following text:
Do. Or do not. There is no try
More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

In JS with conditional if how do I make an output that says "You typed a word, we need a number" or "negative numbers won't work here"? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to have a bunch of numbers asked by prompt, depending on the number there's a message, kind of fortune cookies, but if the user enters a word it needs to say "You typed a word, we need a number", as well as if it is a negative number it will say "negative numbers won't work here"
I know how to do everything but I still can't figure out how to display this part
let number = +prompt("choose a number for good luck")
if (number ===NaN){
document.getElementById("fortuneOutput").innerHTML = "You typed a word, we need a number"
}
else if (number < 0){
document.getElementById("fortuneOutput").innerHTML = "negative numbers won't work here"
}
but none of them work
Use isNaN(), as NaN is never equal to NaN.
Also, parseInt() makes strings to ints/numbers.

My prompt won't return a string, it only returns NaN [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm quite new to this and cannot understand what is going on.
My code is as follows:
let p = prompt("Enter your name, please", "");
let o = prompt("Enter your age, please.", "");
let fname = parseInt(p);
let age = parseInt(o);
let year = (2018 - age);
document.getElementById("p1");
p1.innerHTML = fname + ", you were born in " + year;
When all the is entered on the browser, I end up with something that reads:
"NaN, you were born in 1990"
The number part works, but the string doesn't.
You're running parseInt() on your first name field. Just remove that parseInt call since your name is not an integer!

How can I make this cost calculator script shorter? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
function Method(Distance,Cost,Milage) {
result1=parseInt(Distance/Cost*Milage);
result2=parseInt(Distance/46*Cost);
alert('Your trip will costs '+ result1 +' $');
alert('Your trip in hybrid costs '+ result2 +' $');
if ((result1-result2)<0)
alert('You will save '+(result2-result1)+' $');
else
alert('You will save '+(result1-result2)+' $');
}
}
What can I do to make this shorter?
Yes:
alert("You will save "+Math.abs(result1-result2)+" $");
This will always output the "gap" as a positive number.
As an aside, parseInt is redundant since you are already casting to numbers with the / and * operators.
yes and no, at least you can write shorter :
instead off calling
Method(Distance,Cost,Milage);
everytime.
you could just alert
alert("You will save "+Math.abs((Distance/Cost*Milage)-(Distance/46*Cost)+" $");
everytime

detect country code by a given phone number in javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an object with all available country codes. and I want to know how I can get the country code by a given phone number and display the corresponding country name. the phone number will look like 16041234567(Canada/US +1) or maybe 8601012345678(China +86) or any other country phone number without plus(+) in the front. I just want to get the country code then I know how to display the name. looks like the code can be 1 up to 4 digits.
FIRST ANSWER:
Just a quick thought, why not count from the other side.
take the 10 numbers off the back side of the number. What you are left with will be the truncated country code.
from your example:
num = "16041234567";
code = num.slice(0, num.length-10);
country_name = country_code_object[code];
This code assumes that your object can deal with variable length codes (but you could always buffer the front of the code if you needed to.
BROKEN: China does not use 10 number (cells use 11)
FIX:
After looking into country codes more completely I relised they are a prefix tree. This means that for a liner time you can just check character by character
num = "16041234567";
country_code;
i = 1;
while (!country_code || i < num.length) {
country_code = country_code_obj[num.slice(0, i)];
i++;
}
The nature of country codes will guarantee that the first code that works will be the correct one. (and we can imagine that this must be true, the phone company doesn't know when you are done typing numbers. They just know when you've reached the end of a valid country code)

Categories

Resources