+ 2
JS challenge indexOf() quiz answer.
Was doing JS challenges and stumbled across this quiz many times. I would be grateful if someone provide me the answer. Technically, the while loop will loop forever and break the program. The for version, which I recreated based on the original, will output 2 as the first element (which is not the expected answer). Edit: After 10 times of trial and error, finally got 5 as the correct answer, still don't understand why though. https://code.sololearn.com/WVaIJwEK9aME/?ref=app
13 Respostas
+ 1
I don't believe they automatically do that, as I have submitted a quiz correction in the past and tendered evidence of the correct result. If I remember correctly, there is a button in the upper-right of "check answers" screen that allows you to notify SL of inaccurate quiz answers.
+ 2
Hi Nguyá»
n. Forgive me, but not sure I understand the question. Are you asking why the first value pushed to array is 2, or are you asking why the code output is 5?
Your sample code does not output 5, so I'm assuming your question is really about the first value being 2. If that's the case, you can see it broken down like this:
"Love what you do!".indexOf('',0+1); // returns 1
result.push(1+1); // returns 2
Technically, the string doesn't matter at all, since the position of '' (empty string) is always 0. Then, the 2nd argument specifies the starting position, and you're always returning index+=1. And for the first iteration index was initialized at 0.
Next, you're "pushing" (i.e., adding) to the array index+1, which is now 2.
Does that help?
+ 1
ServantCoder
Sorry if what I said was confusing. I'll summarise it here.
1 How do SoloLearn check the accuracy of submitted challenge quiz?
2. Cuz I screenshoted the quiz and recreated it, which outputs 2. But 2 is not the answer of the quiz, it is 5
+ 1
Ah, problem solved, thanks for the help.
0
Because it's reinitialising the value of index to 1 everytime and in for loop its just initialize once and increment as per condition.
0
Kunal Thakur both the for and while version will have the index incremented indirectly via the indexOf() method. Break the loop when i = 10 in the while loop and you get the same result as the for version.
Even if it get reassigned to 1, it will output 2, and that is still incorrect.
0
For loop work different it initialize only first time and then condition check afterwards.
0
Kunal Thakur Would you mind taking a look at the code? I just added some comparision.
And if so, what is the actual output (result[0])?
0
Kunal Thakur Yes. They work differently, though can still provide equivalent output.
0
if (index >= testString.length) break;
I think I understood wrong but is this multiple sorting character?
0
Eh, where did you get that line? And what is multiple sorting characrer anyway. I'm new, haha.
It would be easier to assume that the creator of this quiz was wrong. Or maybe I'm doing wrong somewhere somehow.
0
Kunal Thakur indexOf() with "" as the search value will return the first value from the specified index. In this case, the specified index is its incremented self, so it works just like increment.
0
Okay sorting character position means
var str="I am a variable with value" ;
How many place "a" index is coming in this string
Output
3,6,9.....