【发布时间】:2022-01-16 16:04:41
【问题描述】:
我现在遇到了一个问题:我需要一个函数来查找由参数接收到的数字中的连续数字组成的最高数字。 例如:如果我的输入是 1235789,我的输出应该是 789。如果我的输入是 123689,我的输出应该是 123。
function getbiggestNumber(numberInput) {
const numberString = numberInput.toString(); // turned into string
const temporaryResult = []; // create the array of possible solutions which i'd go through to find the highest value inside of it
for (let i = 0; i < numberString.length; i += 1) {
const temporary = [numberString[i]]; // create a temporary answer that would serve as a base
for (let x = i + 1; x < numberString.length; x += 1) {
const subResult = Number(numberString[i]) - Number(numberString[x]); // the result of the current number minus the following number
if (subResult === -1) { // if they are in a sequence this should be -1
temporary.push(numberString[x]); // pushing this number to that temporary answer
} // here should be some condition for it to keep running, instead getting into another number of for loop
}
temporaryResult.push(temporary); // pushing that temporary answer to the result, so I could keep track of it
}
console.log(temporaryResult); // checking the output
}
问题是这段代码只在数组中提供两位数,这是我发现的唯一方法。 如果有人可以让我了解这一点,我将非常感激。 谢谢!
【问题讨论】:
标签: javascript algorithm