最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Find longest word in string - JavaScript - Stack Overflow

programmeradmin1浏览0评论

I am currently on a task in python and i need help with this and i am to

Write a function called longest which will take a string of space separated words and will return the longest one.

For example:

longest("This is Andela") => "Andela"
longest("A") => "A"

This is the sample test

const assert = require("chai").assert;

describe("Module 10 - Algorithyms", () => {
describe("longest('This Is Andela')", () => {
    let result = longest("This Is Andela");
    it("Should return 'Andela'", () => {
        assert.equal(result, 'Andela');
    });
});

describe("longest('This')", () => {
    let result = longest("This");
    it("Should return 'This'", () => {
        assert.equal(result, 'This');
    });
});

describe("longest(23)", () => {
    let result = longest(23);
    it("Should return ''", () => {
        assert.equal(result, '');
    });
});
});

This is what i have tried

function longest(str) {
str = "This is Andela";
var words = str.split(' ');
var longest = '';

for (var i = 0; i < words.length; i++) {
  if (words[i].length > longest.length) {
    longest = words[i];
  }
}
return longest;
}

But my code seem to only pass the first test case.Please what do i need to change to pass the other two first case considering i am new to javascript

I am currently on a task in python and i need help with this and i am to

Write a function called longest which will take a string of space separated words and will return the longest one.

For example:

longest("This is Andela") => "Andela"
longest("A") => "A"

This is the sample test

const assert = require("chai").assert;

describe("Module 10 - Algorithyms", () => {
describe("longest('This Is Andela')", () => {
    let result = longest("This Is Andela");
    it("Should return 'Andela'", () => {
        assert.equal(result, 'Andela');
    });
});

describe("longest('This')", () => {
    let result = longest("This");
    it("Should return 'This'", () => {
        assert.equal(result, 'This');
    });
});

describe("longest(23)", () => {
    let result = longest(23);
    it("Should return ''", () => {
        assert.equal(result, '');
    });
});
});

This is what i have tried

function longest(str) {
str = "This is Andela";
var words = str.split(' ');
var longest = '';

for (var i = 0; i < words.length; i++) {
  if (words[i].length > longest.length) {
    longest = words[i];
  }
}
return longest;
}

But my code seem to only pass the first test case.Please what do i need to change to pass the other two first case considering i am new to javascript

Share Improve this question asked May 22, 2017 at 11:27 diagolddiagold 4752 gold badges7 silver badges32 bronze badges 4
  • stackoverflow./a/31037162/622813 – l2aelba Commented May 22, 2017 at 11:33
  • Possible duplicate of Find the longest word in a string using javascript – l2aelba Commented May 22, 2017 at 11:33
  • what will be the output if all words inside string is of same length like :- this code used? – Death-is-the-real-truth Commented May 22, 2017 at 11:44
  • 2 @AlivetoDie Is there really a need to spam every answer with the same ment? – Jamiec Commented May 22, 2017 at 11:44
Add a ment  | 

3 Answers 3

Reset to default 9

You need to remove this line in your function:

str = "This is Andela";

You function should be (added check if str is string):

function longest(str) {
    if(typeof str !== 'string') return '';
    var words = str.split(' ');
    var longest = '';

    for (var i = 0; i < words.length; i++) {
      if (words[i].length > longest.length) {
        longest = words[i];
      }
    }
    return longest;
}

Simplified code with no arrays and no each statements.

function longer(champ, contender) {
  return (contender.length > champ.length) ? contender: champ;
}

function longestWord(str) {
    var words = str.split(' ');
    return words.reduce(longer);
}
console.log(longestWord('This is longest'));
console.log(longestWord('This is longest or may this is more longestest'));

Check this one:

function longest(s){
    return typeof s != 'string' ? '' : s.split(' ').sort( (a,b) => b.length - a.length)[0]
}

The input is converted into string through concatenation with an empty string so that it works when a number is passed as well.

发布评论

评论列表(0)

  1. 暂无评论