繁体   English   中英

JavaScript 变量查找解决方案

[英]JavaScript variable finding solution

function longest(string) {
        var str = string.split(" ");
        var longest = 0;
        var word = null;
        for (var i = 0; i < str.length; i++) {
            document.write(longest);
            if (longest < str[i].length) {
                longest = str[i].length;   
                word = str[i];
            }
        }
        return word;
    }

document.write (longest("Web Development Tutorial"));

我是新手,它是用于识别字符串中最大单词的代码,我在 Stack Overflow 上找到了这个代码,它工作正常,但我无法理解 var long 的工作原理,它如何用于比较 if 语句中的值,因为什么我认为它的值为 0 并且没有添加任何其他内容,因此它是如何工作的。

这里发生了什么

首先

longest = 0

然后

longest = 3 (for 'web')

然后是比较

longest < 11 (for 'development') true
longest = 11;

下一个条件变为假

所以它正在返回这个word

longest = str[i].length;

此行将一个新值设置为最长的变量。 然后在 for 循环的每次迭代中,新值用于比较单词是否比当前最长的单词长。

请在下面找到解释。

 function longest(string) { var str = string.split(" "); // Gives an array of words var longest = 0; // initializing length of longest word to 0 var word = null; for (var i = 0; i < str.length; i++) { // loops over the array of words document.write(longest); // comparing length of each with longest value. If longest is less than the current word length, change the longest word & length if (longest < str[i].length) { // setting the length of the word as longest. Here is the longest variable getting updated longest = str[i].length; document.write(longest); word = str[i]; // setting the longest word } } return word; // returning the longest word set in the above iterator } document.write (longest("Web Development Tutorial"));

此外,您可以使用Array.reduce将逻辑简化为如下

 function longest(string) { // Compare length of each word and return the longest word return string.split(" ").reduce((a, c) => a.length > c.length? a: c); } document.write(longest("Web Development Tutorial"));

检查这个https://www.geeksforgeeks.org/c-program-find-largest-element-array/

function longest(string) {
        var str = string.split(" ");
        // let 'longest' word having 0 character and let 'word' is null for starting point
        var longest = 0;
        var word = null;
        // go through all words
        for (var i = 0; i < str.length; i++) {
            console.log(longest);
            // if current 'word' is having more number of characters then our 'longest' count
            // then set longest count to current word's length 
            // And set word to current word
            if (longest < str[i].length) {
                longest = str[i].length;
                 console.log(longest);   
                word = str[i];
            }
        }
        // as we go through all words then 'longest' will have longest count and 'word' will have cursponding word and return that longest 'word'.
        return word;
    }

 console.log (longest("Web Development Tutorial"));

这个答案看起来简单直观。 它还有一个演示,您可以快速浏览一下并使用它。

链接到答案

 console.log("A Web Development Tutorial and VeryLongWoooooord".split(' ').sort((prevWord, nextWord) => { if(prevWord.length > nextWord.length) { return -1; } if(prevWord.length === nextWord.length) { return 0; } if(prevWord.length > nextWord.length) { return 1; } })[0]);

暂无
暂无

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

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