Return Largest Phone Number In Js Array
Solution 1:
As suggested by fellow friends, you need to wrap the phone numbers within quotes as they are not numbers. Once you have done that, try following logic to extract the desired results for you
functionmyFunction(array) {
// Creates the number array against the telephone numbers var numberArray = array.map(function(phone){
returnparseInt(phone.replace(/\D/g, ''));
});
// Now it is simple to compare and find the largest in an array of numbersvar largestIndex = 0;
var largestNumber = numberArray[0];
for (var i = 0; i < numberArray.length; i++) {
if (largestNumber < numberArray[i]) {
largestNumber = numberArray[i];
largestIndex = i;
}
}
// Fetch the value against the largest index from the phone numbers arrayconsole.log(array[largestIndex]);
}
myFunction(["509 - 111 - 1111", "509 - 222 - 2222", "509 - 333 - 3333", "509 - 333 - 3332"]);
Solution 2:
This is an alternative.
I'd recommend to keep each part of the number in an array (making them an integer).
functionmyFunction(array){
var res="";
for(var i=0,l=array.length;l>l;i++){
res=res+array[i][0]+' - '+array[i][1]+' - '+array[i][2]+'\n'
}
console.log(res)
}
myFunction([[509,111,1111],[509,222,2222],[509,333,3333]]);
Solution 3:
If you want to use some built in JavaScript functions then you can use the map method of arrays to modify each element of the array, and the Math.max method to find the largest number.
functionmyFunction(array) {
var array = array.map(function(elementOfArrayAtIndex) {
returnparseInt(elementOfArrayAtIndex.replace(/\D/g, ''), 10);
});
var largest = Math.max.apply(null, array);
console.log(largest);
}
myFunction(["509 - 111 - 1111", "509 - 222 - 2222", "509 - 333 - 3333"]);
Here's a fiddle http://jsfiddle.net/v3b7gx6L/1/
This is basically what the map method is doing
for(var j = 0; j < array.length; j++) {
array[j] = array[j].replace(/\D/g, '');
}
Solution 4:
You're going to want to pass the numbers as strings, as K phi suggested:
myFunction(["509-111-1111", "509-222-2222", "509-333-3333"]);
First, you'll want to remove the dashes from the string. You can do this with the replace
method, which takes a character to search for, and a character to replace it with. For replace
to match all instances of the dash, we need to use a regular expression, with the g
flag which means "global". We're finding a dash and replacing it with nothing, which amounts to deleting the dashes. In this case, you'll do this:
"509-111-1111".replace(/-/g,'');
=>"5091111111"
You'll then need to turn the strings into Number
s, if you want to compare or add them. You can do this with parseInt(string,radix)
. radix
defines what base to use: in this case you'll use base ten. So your parseInt
call will look like this:
parseInt("5091111111",10);
=> 5091111111
You can perform this process on each string in the arguments, and then compare them to determine the largest one:
var numbers = [];
for (var i=0;i<array.length;i++) {
var numberString = array[i].replace(/-/g,'');
varnumber = parseInt(numberString,10);
numbers[i] = number;
}
var largestNumber = numbers[0];
for (var i=0;i<numbers.length;i++) {
if (numbers[i] > largestNumber) {
largestNumber = numbers[i];
}
}
return largestNumber;
Post a Comment for "Return Largest Phone Number In Js Array"