This is the function and it’s role it’s to return the shortest word out of a string:
function findShort(s) {
s = s.split(" ");
let shortest = s[0];
for (let i = 0; i < s.length; i++) {
if (s[i].length < shortest.length) shortest = s[i].length;
}
return shortest;
}
console.log(findShort("bitcoin take over the world"));
In the end shortest variable will have the value of 4 and not the value of 3, I can’t understand why it doesn’t move to 3 because we will have the condition at some place in time i=3 if(3<4) shortest = 3;
the condition is true so why won’t we have it.
I know the solve to this it’s this:
function findShort(s) {
s = s.split(" ");
let shortest = s[0];
for (let i = 0; i < s.length; i++) {
if (s[i].length < shortest.length) shortest = s[i];
}
return shortest.length;
}
console.log(findShort("bitcoin take over the world"));
But I want to understand why it doesn’t work with the first method?