I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (
</>) will also add backticks around text.
If you put a console.log(n); on the line after the following:
then you will see n is always 0. It is 0, because since a is “hello” and a is “h”, you are finding the index of “hello” which is “h” (which is 0) and then assigning it to n. Your for loop is doing the same thing 3 times. Instead of looking only at the 0 index of a, you should be looking at the ith index of a. See if you can change a to reflect the ith index instead of the 0 index of a.