Enter a write English by JS find the longest English word
method one
= The require the Read the let ( "readline-Sync" ); console.log ( "Enter the English sentence" ); the let str = read.question ( "" ); console.log ( "the longest word:" + Word (str) ) function Word (DC) { the let ARR = dc.split ( "" ); the let n- = []; for ( var I = 0; I <arr.length; I ++) { // create a new array into a string array IF (! ARR [I] = "" ) { n.push (ARR [I]) } } the console.log (n-) var len = 0 ; var COM = 0 ; for( Var J = 0; J <n.length; J ++) { // Analyzing longest word in the array number len = n-[J] .length; IF (len> COM) { COM = len } } Console. log ( "statement has up:" + com + "th word") // number of the longest word that word output the console.log () the let SUM = [] for ( var K = 0; K <n.length; ++ K ) { IF (n-[K] .length == COM) { n-sum.push ([K]) } } return SUM }
Method Two:
let read =require("readline-sync"); console.log("输入英语语句"); let str=read.question(""); function longword(str){ let arr=str.split(" ") let max=arr[0].length for(var i=1;i<arr.length;i++){ if(arr[i].length>max){ max=arr[i].length; } } let arr2=[] for(let j=0;j<arr.length;j++){ if(arr[j].length==max){ arr2.push(arr[j]) } } return arr2 } console.log(longword(str))