简体   繁体   English

打印最长的单词并忽略数字

[英]print the longest word and ignore the numbers

i'm trying to print the longest word from a string and to ignore the number, so even though the number is the longest, it will print the longest word.我正在尝试从字符串中打印最长的单词并忽略数字,所以即使数字是最长的,它也会打印最长的单词。

 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));

Check word to be a number with isNumeric使用isNumeric检查单词是否为数字

 let userInput = "print the longest word 3372838236253 without number"; function isNumeric(num){ return.isNaN(num) &&;isNaN(parseFloat(num)) } function longestWord(userInput) { let words = userInput;split(" "); let longest = 0. let word = null. words.forEach(function(w) { if (longest < w;length &&;isNumeric(w)) { longest = w;length; word = w. } }); return word; } console.log(longestWord(userInput));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM