Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Here's the ful code
JavaScript
<script>
  $(document).ready(function() {

    $("#getMessage").on("click", function() {
      $.getJSON("/json/cats.json", function(json) {

        var html = "";
        // Only change code below this line.
         /*json.forEach(function (itm ) {  html = '<code>'+JSON.stringify(json)+'</code>';  });*/
        json.forEach(function(val) {
  var keys = Object.keys(val);
  html += "<div class = 'cat'>";
  keys.forEach(function(key) {
    html += "" + key + ": " + val[key] + "<br>";
  });
  html += "</div><br>";
});
      
        
        // Only change code above this line.

        $(".message").html(html);

      });
    });
  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
   </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>


can someone explain me how this code is working. It will be awesome if you can add comment to every line. I want to understand what happens after every line of code.

JavaScript
var html = "";
       // Only change code below this line.
        /*json.forEach(function (itm ) {  html = '<code>'+JSON.stringify(json)+'</code>';  });*/
       json.forEach(function(val) {
 var keys = Object.keys(val);
 html += "<div class = 'cat'>";
 keys.forEach(function(key) {
   html += "" + key + ": " + val[key] + "<br>";
 });
 html += "</div><br>";


What I have tried:

I have tried asking it on gitter.im. But wasnt able to understand. I also looked at stackoverfloW. But didnt understand it there.
Posted
Updated 30-Apr-16 11:57am
Comments
Sergey Alexandrovich Kryukov 30-Apr-16 13:31pm    
I'm afraid you won't find a volunteer to do this nonsensical work. It would not help you, no matter how detailed comments are. If you want to learn programming, this would be not a productive way, but totally useless. No matter what you goal is, it makes no sense.
—SA

1 solution

You can learn about JSON and JavaScript from many online sources.
W3Schools is one example that can be good for beginners.

JavaScript Tutorial[^]
JSON Tutorial[^]

Please don't ask people to dig into code you found somewhere on the internet and expect them to explain and comment line by line.
If you want to see what happens when the code executes, use the debugger and/or display the value of certain variables using one of the JavaScript Output[^] methods.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900