zenorocha / voice-elements

:speaker: Web Component wrapper to the Web Speech API, that allows you to do voice recognition and speech synthesis using Polymer
http://zenorocha.github.io/voice-elements
1.36k stars 256 forks source link

[question] voice-recognition: is for-loop on results required? #39

Open masonlouchart opened 6 years ago

masonlouchart commented 6 years ago

There is 2 questions I would like to ask about this snippet.

https://github.com/zenorocha/voice-elements/blob/d01df6683e401be8683bd5d4f2076691a4ecdcff/src/voice-recognition.html#L66-L69

Why the voice-recognition element is working?

Due to the prefix increment of i in the for-loop, I expect a TypeError because cannot read "0" of undefined no matter is the array length. I added a log and the i variable seems incremented after a loop.

capture d ecran 2017-12-07 a 14 42 02

Do we need a for-loop?

I think what we need is simply read the last entry of the results array. AFAIK, the resultIndex variable is the index of the last result received. So the for-loop do 1 loop on the last entry right?

Thanks to help me to understand how it's working. 👨‍🎓