script-for-index2.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const forTable = (arr) => {
  2. let str = "<table style = 'border-collapse: collapse; border: 1px solid; text-align: center; margin-top: 20px;'>";
  3. for (let index of arr.keys()) {
  4. if (index % 2 == 0) {
  5. str += "<tr style='background-color: #babfc4;'>";
  6. } else {
  7. str += "<tr style='background-color: #fcb450;'>";
  8. }
  9. for (let number of arr[index]) {
  10. str += `<td style = 'border-collapse: collapse; border: 1px solid; width: 30px; height: 30px;'>${number}</td>`;
  11. }
  12. str += "</tr>";
  13. }
  14. str += "</table>";
  15. return str;
  16. }
  17. // Currency Table
  18. // Оформите задание Currency Table как функцию, без параметров, которая складывает полученные данные во
  19. // внутренний двумерный массив, после чего отображает его используя функцию из задания For Table
  20. const currencyTable = () => {
  21. fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  22. .then(data => {
  23. let arr = [];
  24. arr[0] = [''];
  25. for (let currency of Object.keys(data.rates)) {
  26. arr[0].push(currency);
  27. }
  28. for (let rowCurrency of Object.keys(data.rates)) {
  29. let rowArr = [rowCurrency];
  30. for (let columnCurrency of Object.keys(data.rates)) {
  31. rowArr.push(+(data.rates[rowCurrency] / data.rates[columnCurrency]).toFixed(2));
  32. }
  33. arr.push(rowArr);
  34. }
  35. document.write(forTable(arr));
  36. })
  37. }
  38. currencyTable();