+ 4
[JAVASCRIPT] For loop /if /iteratons and boolean [SOLVED]
Would someone please be so kind to tell me why I must return true in below code, outside the for, and not inside? https://code.sololearn.com/WzN4di3kzQ1P/?ref=app
5 Answers
+ 7
š¢ Update
Because the for loop was used to iterate the 2nd string, and then there's a condition inside the loop to check whether each character from the 2nd string exists in the 1st array.
If it's not the case, we straightaway return false as we have found that there's at least ONE character exists in the 2nd string but not 1st string.
Otherwise if we never return in the loop and it finished looping already (after/outside the for loop), then we return true to indicate that we have finished checking and ALL characters in 2nd string DO exist in the 1st string.
+ 6
Yes you're right! Therefore if we have reached to the end of string but never return FALSE (at least one character of 2nd string doen't exist in the 1st string), then it must be TRUE.
I think I overlooked the logic before this so please check my updated answer above. š
+ 6
You're most welcome Gustavo! š
+ 5
I did it! I finally got something. Thank you, my friend. I wish you're here so could give you an "abrazo de gol".
+ 4
Zephyr, I'm a bit slow, so I beg you to follow my logic one more time.
I understand that:
1. the for loop was used to iterate the entire character array
3. If it's not the case, we straightaway return false since we know that both strings differ by at least ONE character.
5. then we return true to indicate that we have checked all the characters and there's NO difference between the two.
I partially understand this:
4. Otherwise if we never return in the loop and it finished looping already (after/outside the for loop),
I definetely don't understand this:
2. and then there's a condition inside the loop to check whether each character is equal or not.
Actually, is not there only ONE condition in the loop which checks exclusively for FALSE?
i.e.:
if (arr0.indexOf(arr1[i]) === -1){
return false;<------- there's no 2 checkings. We still need to do something with the TRUES...
}<----so why don't tell the code here to return true, right after finishing the condition?
It wouldn't check -as you say- wether each character is equal or not if the true is right after the } of the if and right before the } of the for?
The solution (which I don't question to be correct) I read it like this:
1. Iterate (for loop) over the second array (arr1) to
2. compare (if) each character (arr0[i]) is contained (indexOf) in the first array (arr0),
3. with the condition being the character strictly (=== ) not in the first array (-1), 4. and then, return false.
5. If you might have the case where there is a character (something like != -1), which is left there if i don't return something to do with it, i return true.
I am correct if i say that in this case, returning true outside the loop, works like a default in a switch? i.e., " code to run if there is no case match"