【发布时间】:2021-04-05 23:01:23
【问题描述】:
我正在尝试打印字符串中最长的单词并忽略该数字, 所以即使数字最长,它也会打印最长的单词。
let userInput = "print the longest word 3372838236253 without number";
function longestWord(userInput) {
let x = userInput.split(" ");
let longest = 0;
let word = null;
x.forEach(function(x) {
if (longest < x.length) {
longest = x.length;
word = x;
}
});
return word;
}
console.log(longestWord(userInput));
【问题讨论】:
-
与
longest < x.length条件一起检查x是否为数字。 Check if a string is a valid number
标签: javascript nan longest-substring