-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
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;
} |
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); |
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
The text was updated successfully, but these errors were encountered: