zoukankan      html  css  js  c++  java
  • Find the Longest Word in a String-freecodecamp算法题目

    Find the Longest Word in a String(找出最长单词)

    1. 要求
      • 在句子中找出最长的单词,并返回它的长度
      • 函数的返回值应该是一个数字。
    2. 思路
      • 用.split(' ')将句子分隔成各个单词组成的数组
      • 定义一个temp变量,将数组第一个元素赋值给它
      • 在for循环中用数组剩余元素的长度对比temp,最后将最大长度的元素赋值给temp
      • 返回temp长度
    3. 代码
      •  1 function findLongestWord(str) {
         2   // 请把你的代码写在这里
         3   str = str.split(' ');
         4   var temp = str[0];
         5   for (var i = 1;i<str.length;i++){
         6     if (temp.length<str[i].length){
         7       temp=str[i];
         8     }
         9   }
        10   return temp.length;
        11 }
        12 
        13 findLongestWord("The quick brown fox jumped over the lazy dog");

      4.相关链接

      • https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/split
      • https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/length
  • 相关阅读:
    接口自动化架构-获取用例
    Windows性能监控工具Perfmon使用指南
    接口自动化架构1-setting
    多进程
    线程锁、守护线程
    多线程
    xlrd模块
    封装写日志的类
    封装redis
    继承
  • 原文地址:https://www.cnblogs.com/ahswch/p/9292844.html
Copyright © 2011-2022 走看看