2
var strings = [ '123-456-7777', '223-456-7777', '123-456-7777' ];
var ints = strings.map(el => el.replace(/-/g, '').split('').reduce((sum, a) => sum + +a, 0));
console.log(ints);

if( ints[0] > ints[1] && ints[0] > ints[2]){
    console.log(strings[0]);
    console.log(ints[0]);
}else if (ints[1] > ints[0] && ints[1] > ints[2]) {
    console.log(strings[1]);
    console.log(ints[1]); 
}else{
    console.log(strings[2]);
    console.log(ints[2]);
};

I have a few questions.

  1. Can I replace this If statement with a switch statement?
  2. I am trying to make the function that prints out the array which has has the largest sum. What other way is there to make this look better?
Tyler
  • 41
  • 2

4 Answers4

0

Math.max.apply(Math, ints) gives you the max value. Along with indexOf you can get the index of the max value.

var strings = [ '123-456-7777', '223-456-7777', '123-456-7777' ];
var ints = strings.map(el => el.replace(/-/g, '').split('').reduce((sum, a) => sum + +a, 0));

console.log(ints);
var max = Math.max.apply(Math, ints);
console.log(strings[ints.indexOf(max)], max); //223-456-7777, 50
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

You don't need switch or if...else. You can use Math.max and pass the array to it calling it using apply.

  1. Remove the hyphens from the array elements
  2. Get the individual digit
  3. Get summation of individual digits in the array
  4. Get the maximum number from the array.

Code:

Math.max.apply(Math, arr.map(str => str.match(/\d/g).reduce((sum, num) => sum + +num, 0)));

var arr = ['123-456-7777', '223-456-7777', '123-456-7777'];
var max = Math.max.apply(Math, arr.map(str => str.match(/\d/g).reduce((sum, num) => sum + +num, 0)));

console.log(max);
document.write(max);

Code Explanation:

str.match(/\d/g).reduce((sum, num) => sum + +num, 0)) will give the summation of individual digits in the main array elements.

arr.map will update the element of each array to the returned value i.e. the summation of individual digits.

Math.max.apply(Math, array) will call the Math.max function and pass the elements of array as individual parameters.

Equivalent code in ES5:

var arr = ['123-456-7777', '223-456-7777', '123-456-7777'];
var max = Math.max.apply(Math, arr.map(function (str) {
    return str.match(/\d/g).reduce(function (sum, num) {
        return sum + +num;
    }, 0)
}));
console.log(max);
Tushar
  • 85,780
  • 21
  • 159
  • 179
0

You can always sort the array and take the max element:

strings.map(function(e){ return e.split('-').reduce(function(a,b){return +a + +b}) }).sort(function(a,b){return b-a})[0];
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
0

Try using for loop, while loop , delete , Array.prototype.sort()

var strings = ['123-456-7777', '223-456-7777', '123-456-7777'];

for (var i = 0, len = strings.length, res = Array(len).fill(0); i < len; i++) {
  var j = strings[i].length, n = -1;
  while (--j > n) {
    if (!isNaN(strings[i][j])) {
      res[i] += +strings[i][j]
    }
  };
  if (res[i - 1] && res[i] > res[i - 1]) {
    delete res[i - 1]
  } else {
    if (res[i] < res[i - 1]) {
      delete res[i];
      res.sort(Boolean)
    }
  };
};

document.body.textContent = res.join(" ")
guest271314
  • 1
  • 15
  • 104
  • 177