Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

根据传入的数字,返回其中包含的最大的连续五位数 #3

Open
erbing opened this issue Apr 16, 2019 · 3 comments
Open

Comments

@erbing
Copy link
Owner

erbing commented Apr 16, 2019

/* 
 * 根据传入的数字,返回其中包含的最大的连续五位数
 * e.g.
 * maxFiveDigits(283910356876)   // =>91035
 * maxFiveDigits(123456) //  => 23456
 */
function maxFiveDigits(num) {
  //TODO:完成该函数
}
@boybird
Copy link

boybird commented Apr 16, 2019

function maxFiveDigits(num) {
    if (typeof num === 'number') {
        if(num<100000){
            return 0;
        }
        num = String(num);
    } else if (typeof num !== 'string' || num.length < 5 || num.match(/^\d+$/) === null) {
        return 0;
    }
    let max = parseInt( num.substr(0,5));
    for(var i=1;i<=num.length-5;i++){
        let it = parseInt(num.substr(i,5));
        if( it>max ){
            max = it;
        }
    }
    return max;
}

@gbldsz
Copy link

gbldsz commented Apr 16, 2019

function maxNum(num) {
  let str = '';
  if (typeof num === 'number') {
    str = num.toString();
  } else {
    return 'please input Number';
  }
  if (str.length <= 5) {
    return parseInt(str);
  }
  let maxNum = 0;
  for (let i = 0; i <= str.length - 5; i++) {
    let num = parseInt(str.slice(i, i + 5));
    maxNum = num > maxNum ? num : maxNum;
  }
  return maxNum;
}
maxNum(283910);

@erbing
Copy link
Owner Author

erbing commented Apr 16, 2019

const maxFiveDigits = (nums) => {
    //TODO:完成该函数
    let num = nums + ''
    if (num.length <= 5) return num
    let temp = 0
    for (let i = 0; i < num.length - 4; i++) {
        const e = num.substr(i , 5);   // substring[start, end)? slice[start, end)? substr(start, length)
        temp = e >= temp ? e : temp
    }
    return Number(temp)
}

export default maxFiveDigits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants