function findLongestWord(str) {
var tim = str.split();
var dav = 0;
for (var i = 0; i < tim.length;)
{
if ( tim[i] > tim[1])
{
tim.replace(tim[1], "");
dav = tim[1].length;
} else {
tim.replace([0], tim[1]);
tim.replace(tim[1], "");
dav = tim[1].length;
}
}
return dav;
}
I’m very new, if anyone can help me understand this issue, I would be very thankful.
I get the message “tim.replace is not a function.”, I understand what that means, but not why it’s relevant.
I know this isn’t the best way to do this challenge, but i have been getting “not a function” messages a lot, and I think I need to understand why before I move on.
Thanks for replying, I’m now reading up on how to use the console, i don’t know how to do that yet. I must have been confused, I thought only the middle part (“conditional”?) was required in the loop, and i could use the next line as a replacement for the iterator?
I think I may need to redo some of the java lessons…
None of the three expressions in the control statement of a for loop are required
for(;;) {
console.log("an infinite loop due to empty control statement")
break
}
for(let i=0;;++i) {
if(i >= 2)
break
console.log("same as using i < 2 as guard in control statement")
}
let n=2
let i=0
for(;i < n;++i) {
console.log("same as setting i=0 in initialization expression in control statement")
}
let n=2
for(let i=0;i < n;) {
console.log("same as using ++i in update expression in control statement")
++i
}
Sorry for the late response. I check tim[0] (first word) against tim[1] (second word),
then i make the bigger word the first in the line and erase the shorter word making the array
one shorter.
You should be comparing the length, and you shouldn’t need to splice the array at all - you should just need to update dav if the length is longer. Missing out conditions in the for loop is super bad style as well; it’s exceptionally difficult to read
Hi @Andrew1,
I am pretty sure I’ve seen this post a while back, and I recall someone saying that there’s got to be an easier way to do this.
So, without giving the punchline away, I would suggest a better approach to this problem would be to turn the string into an array. ‘Sort’ the array based on word length, and grab the first item from the sorted array.
The comparison I was talking about isn’t related to sorting: the OP is trying to find the longest word, comparing strings using > will not give the correct true/false value in this case because that doesn’t compare length.
Edit: Also Math.max(split on spaces -> map string length) I guess is simplest way
comparison is always related to sorting because by definition you can only compare things using inequalities if an order is defined - sorting provides a useful “big picture” to see and understand the effect of different orders that can be applied to the same set of objects
Nono, you misunderstand: the OPs code cannot work properly due to a fundamental misunderstanding about what is being compared. What I’m talking about has literally nothing to do with sorting, it’s related to an error with syntax.
This is valid JS:
"hola" > "hi"
And it is true, but it isn’t true due to “hola” being a longer string than “hi”, it isn’t comparing length
"hola" > "hello"
This is also true
The error appears on this line:
if (tim[0] > tim[1]) {
tim is a list of strings, so this comparison will produce garbage results