Company Financial Ratios integration with JQUERY.

Company Financial Ratios integration with JQUERY.

by Financial Modeling Prep

HTML

<div class="stock-name"></div>

CSS

body {
  font-family: Helvetica Neue, Arial, sans-serif;
  font-size: 14px;
  color: #444;
  margin: 0;
}

.table {
  width: 100%;
  border: solid 1px #DDEEEE;
  border-collapse: collapse;
  border-spacing: 0;
  font: normal 13px Arial, sans-serif;
}

.table thead th {
  background-color: #DDEFEF;
  border: solid 1px #DDEEEE;
  color: #336B6B;
  padding: 10px;
  text-align: center;
  text-shadow: 1px 1px 1px #fff;
}

.table tbody td {
  border: solid 1px #DDEEEE;
  color: #333;
  padding: 10px;
  text-shadow: 1px 1px 1px #fff;
  min-width: 80px;
  text-align: center;
}

/* .stock-name {
  padding: 10px;
} */

JavaScript

$(document).ready(function() {
  var stockName = 'AAPL';
  // https://financialmodelingprep.com/developer/docs
  var url = "https://financialmodelingprep.com/api/v3/ratios/" + stockName + "?period=quarter&apikey=demo";
  $.ajax({
    url: url,
    type: "GET",
    crossDomain: true,
    success: function(response) {

      let resp = response;
      let symbol = resp.symbol;
      let financials = resp;

      let $table = $("<table  class='table'></table>");
      
      for (let i = 0; i < financials.length; i++) {
        let financial = financials[i];
        let $line = $("<tr></tr>");
        let $lineHader = $("<tr></tr>");
        let $head = $('<thead></thead>');

        for (var key in financial) {
          if (i === 0 && financial.hasOwnProperty(key)) {
            $lineHader.append($("<th></th>").html(key));
          }
        }

        $head.append($lineHader);
        $table.append($head);
        let z = 0;
        for (var key2 in financial) {
          if (financial.hasOwnProperty(key2)) {
            if (z === 0) {
              $line.append($("<td></td>").html(financial[key2]));
            } else {
              $line.append($("<td></td>").html('$' + financial[key2]));
            }
            z++;
          }
        }

        $table.append($line);
      }

      $table.appendTo(document.body);

    },

    error: function(xhr, status) {
      alert("error");
    }

  });
});