Closed Bug 1431454 Opened 7 years ago Closed 7 years ago

Tutorial on JavaScript/Guide/Loops_and_iteration has incorrect JavaScript code

Categories

(Developer Documentation Graveyard :: General, enhancement, P1)

All
Other
enhancement

Tracking

(Not tracked)

RESOLVED WORKSFORME

People

(Reporter: boyko.serge, Assigned: fs)

References

()

Details

:: Developer Documentation Request Request Type: Correction Gecko Version: unspecified Technical Contact: :: Details Hi, My second message today. In this page has a bug: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration <script> function howMany(selectObject) { var numberSelected = 0; for (var i = 0; i < selectObject.options.length; i++) { if (selectObject.options[i].selected) { numberSelected++; } } return numberSelected; } var btn = document.getElementById('btn'); btn.addEventListener('click', function() { alert('Number of options selected: ' + howMany(document.selectForm.musicTypes)); }); </script> should be: <script> var numberSelected = 0; function howMany(selectObject) { for (var i = 0; i < selectObject.options.length; i++) { if (selectObject.options[i].selected) { numberSelected++; } } return numberSelected; } var btn = document.getElementById("btn"); btn.addEventListener("click", function(){ alert('Выбрано элементов: ' + howMany(document.selectForm.musicTypes)) }); </script> P.S. var numberSelected = 0; // should be out of function, if not, always the same number ('1') returns
Priority: P1 → --
Summary: bug in tutorial → Tutorial on JavaScript/Guide/Loops_and_iteration has incorrect JavaScript code
Priority: -- → P1
Assignee: nobody → fscholz
Hi, sorry for not coming back to you earlier. I've checked both the English and the Russian documentation and it works correct for me. It is better to have the variable inside the function and not in the global scope. Please reopen with more details on what is not working. Do you get any error message in the console?
Status: UNCONFIRMED → RESOLVED
Closed: 7 years ago
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.