I am currently working on javascript function but these programe make me little bit confuse kindly please explain whole about the program given below, confusing towards for loop decrement.
var socialMediaIcon = {
facebook: "http://facebook.com/haseebjumani",
Twitter: "http://twitter.com/haseebjumani",
whatsapp: "http://whatsapp.com/",
flickr: "http://flicker.com/haseebjumani"
}
var social = function() {
var output = '<ul>',
myList = document.querySelectorAll(".social-media");
for (var key in arguments[0]) {
output += '<li><a href="'+ socialMediaIcon[key] + '">' +
'<img src="images/' + key + '.png" alt="icon for' + key + '">' + '</a></li>';
}
output += '</ul>';
for(var i = myList.length - 1; i >= 0; i--){
myList[i].innerHTML = output;
};
}(socialMediaIcon);
<!DOCTYPE html>
<head>
<title>Social Media Icons</title>
</head>
<body>
<nav class="social-media">g</nav>
<h1>Welcome</h1>
<p> lorem ipsum delor sit amit lorem ipsum delor sit amit lorem ipsum delor sit amit
lorem ipsum delor sit amit lorem ipsum delor sit amit lorem ipsum delor sit amitlorem ipsum delor sit amit
lorem ipsum delor sit amit lorem ipsum delor sit amit lorem ipsum delor sit amit lorem ipsum delor sit amit
</p>
<nav class="social-media"></nav>
<script src="social.js"></script>
</body>
</html>
The problem here is that
<nav class="social-media"></nav>
is used twice over here. So when you reach at this point of codefor(var i = myList.length - 1; i >= 0; i--)
it starts looking for myList in code which you have defined asmyList = document.querySelectorAll(".social-media");
If you have a close look in your for loop initialisation you will find follwing:Other method if you want to use i++
Please initialisation your for loop as :