- -1 is returned when you don’t find the value.
- The loop says for each character (here called value) inside chArray try to locate another identical character in targetStr, if you don’t find another add the character to targetStr. So basically if you enter hello it would go like this:
value = h -> try to find another h -> gets -1 (so no other h) -> add h to the return
value = e -> try to find another e -> gets -1 (so no other e) -> add e to the return
value = l -> try to find another l -> gets -1 (so no other l) -> add l to the return
value = l -> try to find another l -> gets 2 (position of l) -> don’t add l
value = o -> try to find another o -> gets -1 (so no other o) -> add o to the return
- You can’t do targetStr.indexOf() or targetStr += value if targetStr is not initialized and can’t return a non-initialized variable
0
solved What does “if (targetStr.indexOf(value) == -1) ” condition mean in the below program, so we get correct output [closed]