return largest phone number in JS array

2019-01-20 16:33发布

问题:

I have to return the largest phone number,not index, from an array in Javascript. I am trying to remove the non digit characters then find largest number. I am new and not experienced with the syntax. Alot of examples show parts of my problem. I don't know how to connect them into one function and how to return the answer. Please help me out. Heres what I have:

function myFunction(array) {
    var largest = array.replace(/\D/g, '');
    for (var i = 0; i < array.length; i++) {
        if (largest < array[i]) {
            largest = array[i];
        }
    }
    console.log(largest);
}

myFunction([509 - 111 - 1111, 509 - 222 - 2222, 509 - 333 - 3333]);

回答1:

This is an alternative.

I'd recommend to keep each part of the number in an array (making them an integer).

function myFunction(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]]);


回答2:

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

function myFunction(array) {

  // Creates the number array against the telephone numbers 
  var numberArray = array.map(function(phone){
     return parseInt(phone.replace(/\D/g, ''));
  });


  // Now it is simple to compare and find the largest in an array of numbers
  var 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 array
  console.log(array[largestIndex]);
}

myFunction(["509 - 111 - 1111", "509 - 222 - 2222", "509 - 333 - 3333", "509 - 333 - 3332"]);


回答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.

function myFunction(array) {
    var array = array.map(function(elementOfArrayAtIndex) {
        return parseInt(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, '');  
}


回答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 Numbers, 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,'');
    var number = 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;