DRAG DROP
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.)
Answer: See the 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];
}
The answer is missing a part, between Box 1 and Box 2 should be:
languages = $.makeArray(items);
you don’t have to do that. Languages is an array and so already has the built-in prototype function (push, sort, etc.). You would have to do that if you were trying to invoke built in array function on the ‘items’ NodeList however
although i do agree they could have just used
languages = $.makeArray(items);
instead of the step 2 they have provided.
I agree 100% with Zachary last comment, what’s more it would be more efficient that the provided solution!
Answer is not correct.
See http://www.aiotestking.com/microsoft/which-four-actions-should-you-perform-in-sequence-209/
https://plnkr.co/edit/6mOr8Hl0qAUs0ydnIG08 – if you change Box 2/Step 2 with “languages = $.makeArray(items);”, the result is not the same/not correct.