需要帮助返回字符串中最大的3个连续数字(javascript)

时间:2019-04-09 12:48:08

标签: javascript string numbers

在函数中,我得到了一串数字作为参数。我需要找到最大的3位数连续数字并将其返回。

function largestThreeDigitNum(numString){

    let largestNumber = 0;
    let largestThreeNumber = 0; 

    for(let i= 0; i<numString.length; ++i){
        if(numString[i] === largestNumber){
            largestThreeNumber = largestNumber++
        }
    }
    return largestThreeNumber; 
}

4 个答案:

答案 0 :(得分:0)

好的,我们假设您输入的字符串类似于“ 123 465 789”。您可以将其拆分为空格,仅保留3位数字,对其进行排序,最后3位。

largestThreeDigitNum = numString => numString
                                       .split(" ") // split it over spaces
                                       .filter(n => n.length === 3) // keep only 3 digit numbers
                                       .sort()
                                       .slice(-3) // take the last three

console.log( largestThreeDigitNum("56 456 321 78 987 123 741 2 96 41 58 654") )

答案 1 :(得分:0)

这是一个返回三个最大连续数字的小脚本,希望对您有帮助

function largestThreeDigitNum(numString) {
  var numbers = [];
  
  // Loop to fill an array with all 3 consecutive numbers
  for (var i = 0; i < numString.length - 2; i++) { 
    numbers.push(numString.slice(i, i + 3));
  }

  numbers.sort() // sort the array from the smallest to the biggest
  return numbers[numbers.length - 1] // return the last item of the array, the biggest one
}

console.log(largestThreeDigitNum("693759936") )

答案 2 :(得分:0)

这是一种冗长的方法,试图将问题分解为几个小问题,并使用小功能解决这些问题。本质上,我们:

  • 将字符串转换为数组
  • 将数组拆分为三个或尽可能接近三个的块
  • 过滤掉不是三个数字长的块
  • 将3个“数字”字符串的块转换为单个数字
  • 仔细检查每个数字,记录最大的数字
  • 返回最大的

如果您需要快速的解决方案,那么这不是最有效的解决方案,您最好选择其他解决方案。但是,如果您对采用可组合的小块构建的解决方案感兴趣,那么这可能对您有益。

const splitIntoAllChunks = size => array => Object.values(
  array
    .reduce((prev, _, i) => ({
      ...prev,
      [i]: array.slice(i, i + size),
    }), {})
);
  
const splitIntoThrees = splitIntoAllChunks(3);

const isLength = length => array => array.length === length;

const stringArrayToNumber = array => Number(array.join(''));

const getMax = (champ, challenger) => challenger > champ ? challenger : champ;

const example = "184727158129123"

const result = splitIntoThrees(example.split(''))
  .filter(isLength(3))
  .map(stringArrayToNumber)
  .reduce(getMax, 0);
  
console.dir(result);

如果您想使用更像原始解决方案的东西,则可以使用以下类似方法对其进行修复:

const testData = "1234596789"

function largestThreeDigitNum(numString){

    // this could be passed into the function instead, that way
    // the same code could be used for numbers of different sizes
    let numberSize = 3;

    // we only care about the largest three digit number at any given time
    let largestThreeNumber = 0; 

    // I've changed this to i++ as it's a little more conventional that way
    // I've also setup the loop to stop as soon as it can
    for (let i = 0; i < numString.length - (numberSize - 1); i++){
 
        // find the number for comparison
        const thisNumber = Number(numString.slice(i, i + 3))
        
        // if it's the biggest number, keep track of it
        if(thisNumber > largestThreeNumber){
            largestThreeNumber = thisNumber;
        }
    }
    return largestThreeNumber; 
}

console.dir(largestThreeDigitNum(testData))

答案 3 :(得分:0)

您可以使用一个临时数组并检查值。

var array = [101, 102, 103, 307, 308, 309, 211, 215, 227, 249, 479, 480, 481, 901, 902, 635, 636, 637],
    temp = [],
    largest = array.reduce((r, b, i, { [i - 1]: a }) => {
        if (a !== b - 1) {
            temp = [b];
            return r;
        }
        temp.push(b);
        if (temp.length > 3) temp.shift();
        if (temp.length === 3 && (!r || r[0] < temp[0])) return temp.slice();
        return r;
    }, undefined);

console.log(largest);