简体   繁体   English

使用javascript / html格式查找最长的单词

[英]find the longest word using javascript / html form

I tried to run the code but nothing is showing on my page. 我试图运行代码,但是页面上没有任何显示。 i'm not sure where the mistakes are. 我不确定错误在哪里。 I tried typing javaScript code to find the longest word in a html form/input,then showing the output on the html body. 我尝试键入javaScript代码以在html表单/输入中找到最长的单词,然后在html正文上显示输出。

 function fnLongestWord(string){ var words = str.split(" "); console.log(words); var findlongest=document.forms["Longestword"], var longest = ""; for(let i=0; i < findlongest.length; i++){ console.log(findlongest[i]); } if ( longest.length > findlongest.length) findlongest = longest; } console.log(longest); document.getElementById("showResult1") = "Number of vowels: "+ longest; 
 <div id="LongWord" class="Tab"> <form id="Longestword"> <label>Enter text: <input name="text "></label> <button type="button" onclick="fnLongestWord()"> Find longest word</button> </form> <!--here the output show--> <p id="showResult1"></p> </div> 

Errors ;Here you are calling fnLongestWord but not passing any argument while fnLongestWord expects a value 错误;在这里您正在调用fnLongestWord ,但未传递任何参数,而fnLongestWord需要一个值

var words = str.split(" "); str is no where defined inside the function str在函数内部没有定义的地方

You need to put this line document.getElementById("showResult1") = "Number of vowels: "+ longest; 您需要把这一行document.getElementById("showResult1") = "Number of vowels: "+ longest; inside the function and this is an invalid assingment. 在函数内部,这是无效的提示。 You need to use innerHTML and assign the value to it 您需要使用innerHTML并为其分配值

 function fnLongestWord(string) { var str = document.getElementById('input').value || string var words = str.split(" "); var longest = words.sort((a, b) => { return b.length - a.length; }) document.getElementById("showResult1").innerHTML = "Number of vowels: " + longest[0]; } 
 <div id="LongWord" class="Tab"> <form id="Longestword"> <label>Enter text: <input id = 'input' name="text "></label> <button type="button" onclick="fnLongestWord()"> Find longest word</button> </form> <!--here the output show--> <p id="showResult1"></p> </div> 

You've got a few mistakes in your code that need fixing. 您的代码中有一些错误需要修复。

Firstly, you call fnLongestWord() when you click the button, thus you are not passing in the string from the form. 首先,您在单击按钮时调用fnLongestWord() ,因此您没有从表单中传递字符串。 You need to get the string from the form by using: 您需要使用以下方法从表单中获取字符串:

var str = document.getElementById('longestWord').value;

This will get the value (the text) of the element with the id longestWord . 这将获取id longestWord的元素的value (文本)。 This will get the text from the textbox (as I've given it the id="longestWord" ) 这将从文本框中获取文本(因为我给了它id="longestWord"

Now you want to loop over your array of words . 现在,您要遍历words数组。 You can use words.length in the for loop to do this. 您可以在for循环中使用words.length来执行此操作。

Next, you want to fix your if statement. 接下来,您要修复if语句。 Currently your syntax and logic are incorrect. 当前,您的语法和逻辑不正确。 Instead, you need to make it if(longest.length < words[i].length) longest = words[i]; 相反,您需要使其成为if(longest.length < words[i].length) longest = words[i]; which reads that if the longest word currently found is smaller than our current word, set the new longest word equal to the current word ( word[i] ). 读取结果是,如果当前找到的最长单词小于当前单词,则将新的最长单词设置为等于当前单词( word[i] )。

Lastly, you're not adding the answer to the page correctly. 最后,您没有将答案正确添加到页面中。 Instead, you should do: 相反,您应该执行以下操作:

document.getElementById("showResult1").textContent += "Longest word is: " + longest;

To set the longest word into the showResult1 paragraph. showResult1段落中设置最长的单词。

See working example below: 请参见下面的工作示例:

 function fnLongestWord() { var str = document.getElementById('longestWord').value; var words = str.split(" "); var longest = ""; for (let i = 0; i < words.length; i++) { if (longest.length < words[i].length) longest = words[i]; } document.getElementById("showResult1").textContent += "Longest word is: " + longest; } 
 <div id="LongWord" class="Tab"> <form id="Longestword"> <label>Enter text: <input id="longestWord" name="text "></label> <button type="button" onclick="fnLongestWord()"> Find longest word</button> </form> <!--here the output show--> <p id="showResult1"></p> </div> 

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

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