This problem was a tricky one. Despite its "easy" rating, I struggled with it for a little thinking of the most roundabout ways of answering, including multiple nested for loops-- which as you can guess was just a joy ride. Maybe possible, but that way would have taken me forever to write and would have probably been very error prone. The hardest part for me turned out to be knowing how to set it up. How could I solve this so that I didn't need to go through every character of a string? Well, you don't want to go through every character necessarily (my first mistake), but rather every word. But I'm getting ahead of myself...
Problem: 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 'split' method to aid you in your quest.
Game Plan:
>> Function that has one parameter (string) in this example.
>> How can you quickly separate each word? Split method. The result of which will return an array of all the words. You'll want to set this equal to some variable for easy access later.
>> For loop to go though each word to determine if it's the longest.
>> This is where I got sort of lost-- I knew I needed to do all the above steps, but I didn't know how to keep track of which word was longest. If you had one independent variable to compare each word to, wouldn't that be easier than making some sort of March-Madness bracket system where you compare every word to each other? Methinks yes. So in order to do that, I'll set a variable equal to 0. The way the loop will work : compare the first word's length to 0, if the word is bigger (which it will be since it will be at least one letter character), i'll save the length of that word to the variable. As the loop progresses it will compare each word to this variable, saving the larger of the two.
>> if statement to compare lengths
>> Return longest word
Step 1:
function longestWord(string) {
}
Step 2:
function longestWord(string) {
var words = string.split(" ");
}
Make sure there's a space between your quotes so that it knows where to split the string. If you didn't include the space, you would get each individual character at each index.
Step 3:
function longestWord(string) {
var words = string.split(" ");
var longest = 0;
}
We declare this variable, so that we don't need to compare each word to each other, but rather one independent variable.
Step 4:
function longestWord(string) {
var words = string.split(" ");
var longest = 0;
for (var i = 0; i < words.length; i++) {
}
}
Remember when you split the string, it results in an array of all the words so we can use the array's length.
function longestWord(string) {
var words = string.split(" ");
var longest = 0;
var longIndx = 0;
var longIndx = 0;
for (var i = 0; i < words.length; i++) {
if (words[i].length > longest) {
longest = words[i].length;
longIndx++;
}
}
}
The tricky part-- if the length of the word at index i is greater than longest (0), then we change the value of longest to the length of that word. AND since we want to keep track of which word we are on, we can set a marker of sorts at 0 (longIndx) that will increase if the word at position i is greater than longest. The value will let us know the index number of the longest word so that we can call it later.
function longestWord(string) {
var words = string.split(" ");
var longest = 0;
var longIndx = 0;
for (var i = 0; i < words.length; i++) {
if (words[i].length > longest) {
longest = words[i].length;
longIndx++;
}
}
return words[longIndx];
}
Try it yourself!
Check out past problems here:
No comments :
Post a Comment