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

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)

Related

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.

Javascript eval() function not working properly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm currently making a calculator with HTML, CSS and Javascript, for practice. I found out that the "built in" eval function did the math from a string. But it doesn't work properly.
I don't know what the problem is. But when i for example do: 11+11/2 which should be 11. Becomes 16.5 for some reason. I have no idea why. I would really appreciate some help.
Here is the code:
function revealAnswer(){
var math = document.getElementById("numbersInputted");
math.innerHTML += " = " + eval(math.innerHTML);
}
There are a whole bunch of reasons why this is the wrong approach.
First, innerHTML returns a string containing, not only the text content of an element, but also any nested HTML elements as well. If it's just the text you want, use textContent.
Next, by having the user input the actual math operator they want to use in the same string with the numbers creates more confusion. Have the user enter that separately and then you can use if/then logic to ultimately use the correct operator.
Next (and this is the important part), don't ever use eval(). It is not required to solve just about any problem you could encounter, but it opens up the door to "Cross Site Scripting" attacks on your website. Additionally, it manipulates the this binding and executes its code in its own scope.
What you really need to do is simply convert the string input into a number so that you can do math with it. You can do this with parseInt() and parseFloat()
So, your line could be:
math.innerHTML += " = " + parseFloat(math.textContent);
Lastly, for math, the order of operations is:
Parenthesis
Exponents
Multiplication
Division
Addtion
Subtraction
You can see that division is done prior to addition and that means that in your expression: 11 + 11/2, first 11/2 is evaluated (5.5) and then it is added to 11 (16.5).
Finally, remember that the + operator in JavaScript can mean mathematical addition or string concatenation and if one of the operands is a string, the other will be converted to a string.

algorithm sum and group by Array data [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 need some help., i am using vb.net and javascript.
I have dataset like this,
Row Price ProductID Region
1 100 12345 JK1
2 100 12345 JK2
3 100 23456 JK1
4 100 23456 JK2
i need the result JK1 = 200 and JK2 = 200. Result is SUM all price with grup by region. May be in SQL this is so simple, but in programming i haven't try.
I need the algoritm in vb.net or javascript. The problem is, the region have a lot of variation that i dont know how much that is.
can some one help?
I like to use Dictionaries for this purpose. I don't really have much experience with datasets but it doesn't really matter for this example. I will just define input as a List(Of RegionPrice) with
Structure RegionPrice
Dim Region as String
Dim Price as Integer
End Structure
[...]
Dim Data as List(Of RegionPrice) = ....
Just loop through your data and get the Region and Price of the current item. Then
Dim dictSum as New Dictionary(Of String, Integer)
For i = 0 to Data.Count - 1
If dictSum.ContainsKey(Data(i).Region) Then
dictSum(Data(i).Region) += Data(i).Price
Else
dictSum.Add(Data(i).Region,Data(i).Price)
End If
Next
You will then have the sums you want for each region in the Dictionary.
Maybe that helps for these kinds of problems.
DataTable.Compute Method can help you:
Dim sumPrice As Object = dataTable.Compute("Sum(Price)", "Region ='" + "JK1" + "'")

How can you replace all occurrences of this token in a String? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
I have a String that looks like this:
"My favorite color is {color:tan}."
I'm trying to figure out how to write a javascript function that replaces all occurrences of the token with whatever color I pass in, but defaults to "tan" if the String is too long.
E.g.
var string = "My favorite color is {color:tan}.";
var favoriteColor = "red";
var maxLength = 25;
var tokenPrefix = "color"
var result = replaceToken(string, favoriteColor, maxLength, tokenPrefix);
Now "result" should equal "My favorite color is red.", because the string is 25 characters long.
But if I set favoriteColor = "purple", then result should equal "My favorite color is tan.", because the word "purple" would cause the string to be longer than 25 characters.
I'm not familiar with regex, so I'm not sure if this is possible in regex, but I've tried to do this using an indexOf() to find the token, but there may be multiple instances. I also tried a replaceAll(), but because it's not a simple string, it won't really work.
There are definitely ways to optimize this, but as a quick one off, here is an example of how to do it:
replaceToken(string, favoriteColor, maxLength) {
tokens = string.match(/{(.*?)}/)
color = tokens[1].split(':')[1]
if (favoriteColor.length < maxLength) string = string.replace(tokens[0], favoriteColor)
else string = string.replace(tokens[0], color)
return string
}

Remove () and - and white spaces from 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 a phone number like (123) 456-7891. I need number like 1234567891.
How can I do that in Javascript ?
To be on the safe side, I would recommend removing everything except + (for country codes) and digits:
result = subject.replace(/[^+\d]+/g, "");
You can use String.replace() to do this. Pass it a RegExp that matches everything but digits and replace them with ''.
var number = '(123) 456-7891';
number = number.replace(/[^\d]/g, '');
alert("(123) 456-7891".replace(/[\(\)\-\s]+/g, ''));
please check this link.it might help you
http://www.w3resource.com/javascript/form/phone-no-validation.php
there are many examples here.it might be useful for you.

Categories

Resources