Created
September 3, 2017 17:05
-
-
Save jochri3/28aee7fdc7cacf2c7661d9fc8be935e1 to your computer and use it in GitHub Desktop.
null created by ChrisLis - https://repl.it/H7HZ/49
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Write a method that takes in a string. Return the longest word in | |
// the string. You may assume that the string contains only letters and | |
// spaces. | |
// | |
// You may use the String `split` method to aid you in your quest. | |
// | |
// Difficulty: easy. | |
function longest_word(sentence) { | |
var phrase=sentence.split(" ") | |
var first=phrase[0]; | |
for(var i=1;i<phrase.length;i++) | |
{ | |
if(phrase[i].length>first.length) | |
{ | |
first=phrase[i] | |
} | |
} | |
return first | |
} | |
console.log(longest_word("short longest")) | |
// These are tests to check that your code is working. After writing | |
// your solution, they should all print true. | |
console.log("\nTests for //longest_word") | |
console.log("===============================================") | |
console.log( | |
'longest_word("short longest") == "longest": ' + | |
(longest_word('short longest') == 'longest') | |
) | |
console.log( | |
'longest_word("one") == "one": ' + | |
(longest_word('one') == 'one') | |
) | |
console.log( | |
'longest_word("abc function abcde") == "abcde": ' + | |
(longest_word('abc function abcde') == 'abcde') | |
) | |
console.log("===============================================") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment