PrepAway - Latest Free Exam Questions & Answers

Which four actions should you perform in sequence?

You develop an HTML5 webpage. You have the following HTML markup:

You also have the following JavaScript variable defined:

var languages = [];
You need to add statements to an existing JavaScript function to sort the list items.
Which four actions should you perform in sequence? (Develop the solution by selecting the required code segments and arranging them in the correct order.)
Select and Place:

PrepAway - Latest Free Exam Questions & Answers

Answer:

Explanation:
Note:

* getElementsByTagName
The getElementsByTagName() method accesses all elements with the specified tagname.
* Example:
// Get the list items and setup an array for sorting
var lis = ul.getElementsByTagName(“LI”);
var vals = [];
// Populate the array
for(var i = 0, l = lis.length; i < l; i++)
vals.push(lis[i].innerHTML);
// Sort it
vals.sort();
// Sometimes you gotta DESC
if(sortDescending)
vals.reverse();
// Change the list on the page
for(var i = 0, l = lis.length; i < l; i++)
lis[i].innerHTML = vals[i];
}

4 Comments on “Which four actions should you perform in sequence?

  1. Juro says:

    // Add your javascript here
    $(function() {

    var languages = [];
    var items = document.getElementsByTagName(‘li’);

    for (var l = 0; l < items.length; l++) {
    languages.push(items[l].innerHTML);
    }

    languages.sort();

    for (var i = 0; i < languages.length; i++) {
    items[i].innerHTML = languages[i];
    }

    });




    1



    0
    1. anii says:

      Well, as I can see here, the answer given is not correct.

      for loop HAS TO BE in this form “for(var i=0; i use the same letter for the variable in one for loop & use different letters for each for loop.




      0



      0
  2. anii says:

    Sorry, something happened with my comment. I meant:

    for(var i=0; i<items.length; i++)
    We have to use the same letter for the variable AND use different letter for the other for loop.




    0



    0

Leave a Reply