I keep getting an error when I input the following into Sublime:
function longestWord(str) {
var result = str[0];
str = str.split(' ');
for (var i = 0; i < str.length; i++) {
if (str[i].length > result.length) {
result = str[i];
}
}
return result;
}
var sentence = ["the quick brown fox jumped over the lazy dog."];
console.log(longestWord(sentence));
I keep getting an error when I input the following into Sublime:
function longestWord(str) {
var result = str[0];
str = str.split(' ');
for (var i = 0; i < str.length; i++) {
if (str[i].length > result.length) {
result = str[i];
}
}
return result;
}
var sentence = ["the quick brown fox jumped over the lazy dog."];
console.log(longestWord(sentence));
I get an Uncaught TypeError: str.split is not a function(...)
What am I doing wrong? Thank you!
Share Improve this question edited May 27, 2016 at 1:21 timolawl 5,56415 silver badges29 bronze badges asked May 27, 2016 at 1:06 MikeMike 111 gold badge1 silver badge2 bronze badges 7- 1 "What am I doing wrong?" --- you're trying to invoke something that is not a function. – zerkms Commented May 27, 2016 at 1:07
-
what do you pass in as
str
? – Daniel A. White Commented May 27, 2016 at 1:09 -
What is
str
? Give an example as to how you're callinglongestWord('what are you passing here?')
– mferly Commented May 27, 2016 at 1:11 - 3 Why are you passing an array to the function when it expects a string? – Felix Kling Commented May 27, 2016 at 1:28
- 2 @Mike - That's the kind of info that would be rather helpful up front, FYI. – mferly Commented May 27, 2016 at 1:44
6 Answers
Reset to default 3Your problem is that you are passing an argument of an array of one string into your function, rather than just passing in your string.
Also note that there's nearly always a better way than using a for-loop. Array.prototype.reduce is for traversing an array and returning a single value:
var longest = function(str){
return str.split(' ').reduce(function(long, cur){
return cur.length > long.length ? cur : long;
}, '');
};
var str = 'the quick brown fox jumps over the lazy dog';
console.log(longest(str));
Notice you don't have to manually keep track of an index, create temporary variables, or manually extract data from the array. Once you get used to reduce
it can make your code clearer and help prevent mistakes.
function longestWord(str) {
var result = str[0];
str = str.split(' ');
for (var i = 1; i < str.length; i++) {
if (str[i].length > result.length) {
result = str[i];
}
}
return result;
}
var sentence = "the quick brown fox jumped over the lazy dog.";
document.write(longestWord(sentence));
you are trying to split a array, not a string. The below code is corrected.
function longestWord(str){
var result = str[0];
str = **result**.split(' ');
for(var i = 0; i < str.length; i++){
if(str[i].length > result.length){
result = str[i];
}
}
return result;
}
Slightly more functional approach that's a bit more reader-friendly.
function getLargestWord(stringOrArray){
var words = stringOrArray.toString().match(/\w+/gi); //toString..ifs its an array otherwise dont need it
var wordLength = words.map((e ,i) => i = e.length);
var highestNumber = Math.max.apply(null,wordLength);
return words[wordLength.indexOf(highestNumber)];
}
var sentence = "the quick brown fox jumped over the lazy dog.";
console.log(getLargestWord(sentence))
I came across this same longest word challenge and I didn't find a simple way out so I created a method to split a sentence into an array, since str.split() didn't work on that particular platform.
function splitArray(sentence){
var arrayPosition = 0;
var oneWord = "";
var newSentence = sentence + " ";
var splitArray = new Array();
for(var j = 0; j < newSentence.length; j++){
if(newSentence[j] === " "){
splitArray.push(oneWord);
arrayPosition++;
oneWord = "";
}else{
if(!isNaN(newSentence[j])){
//don't add to the string
}else{
oneWord += newSentence[j];
}
}
}
return splitArray;
};
This function returns an array each word in the sentence passed in and doesn't take integers as character or string.
This is because you are trying to split an array.
Instead pass a string.
function longestWord(str) {
var result = str[0];
str = str.split(' ');
for (var i = 1; i < str.length; i++) {
if (str[i].length > result.length) {
result = str[i];
}
}
return result;
}
var sentence = "the quick brown fox jumped over the lazy dog.";
document.write(longestWord(sentence));