Print a table of the top 20 coins/tokens exercise

Assignment: Using HTML and JS

Exercise.html
<html>
<head>
    <title>Fetching API</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <table id="Coins20">
        <tr>
            <td>Rank</td>
            <td>Name</td>
            <td>Ticker</td>
            <td>New Coin</td>
        </tr>
    </table>
    <script>
        $(document).ready(function(){
            fetch("https://api.coinpaprika.com/v1/coins")
            .then(response => response.json() )
            .then(coins => {
                for(let i = 0; i < 20; i++){
                    let coin = coins[i];
                    $('#Coins20').append(
                        `<tr>
                        <td>${coin.rank}</td>
                        <td>${coin.name}</td>
                        <td>${coin.symbol}</td>
                        <td>${coin.is_new}</td>
                        </tr>`
                    )
                }
            });
        })
    </script>    
</body>
</html>

Additional(Not yet working): I’ve done this in a test project before for an interview (WebSockets instead of APIs) Unfortunately, I failed, but I learned a lot.

IDK if this would work but I started off with this then pass the props onto the App.js and import the CoinTable.jsx there as well. coins can be destructured as well into other components

CoinTable.jsx
import React from 'react'

export default function CoinTable({coins}) {
    return (
        <>
            <p>{coins}</p>
        </>
    )
}

export const getServerSideProps = async () => {
    const res = await fetch(`https://api.coinpaprika.com/v1/coins`)
    const coins = await res.json()
  
    return {
        coins
    }
  }

I would love to hear feedback and corrections! :slight_smile:

1 Like

Ok, here’s my solution.
Used HTML + CSS + jQuery this time.
Also I like to use async/await.

https://codepen.io/rego350/pen/PopaweJ

2 Likes

Here is the code : https://codesandbox.io/embed/black-fog-zpbd7

xtxt

3 Likes

https://codesandbox.io/s/crimson-framework-y91ry?file=/index.htmlScreen Shot 2021-06-12 at 10.25.03 AM

2 Likes

var node = document.querySelector(.js-container);
var text = [];
fetch(‘https://api.coinpaprika.com/v1/coins’)
.then( response => response.json() )
.then( coins => {
for (let i = 0; i < 5; ++i ) {
const coin = coins[i];
text.push( <li>${coin.name}: ${coin.symbol}</li> )
}
node.innerHTML = <ol>${text.join('')} </ol>;
} );

2 Likes

https://codepen.io/JulianLN97/pen/YzZbBLe

2 Likes

Well, I learned how to embed a codepen on the forum :slight_smile:

1 Like

Splendid! Keep up the amazing work! :partying_face:

Added some bootstrap CSS and jQuery to inject the JS into the table. She a bit ugly, but she be functional. Could do more with the styling but maybe later :laughing:

Data fetched from the ticker API. It’s a bit slow considering there’s significantly more data being fetched but more functional.
Also extended it out to 50 coins, because more is better right… :skull_and_crossbones:

Would :heart: some feedback

Here’s the URL if you wanna check it out:
https://cbqys.csb.app/

HTML Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Top 50 coins</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
  <h1>Top 50 Coins</h1>
  <p>Top Coins ordered by Market Cap</p>
  
  <table class="table">
    <thead>
         <th scope="col">Rank</th>
         <th scope="col">Name</th>
         <th scope="col">Ticker</th>
         <th scope="col">Price</th>
         <th scope="col">1h</th>
         <th scope="col">24hr</th>
         <th scope="col">7d</th>
         <th scope="col">24hr Volume</th>
         <th scope="col">Market Cap</th>
    </thead>
    <tbody id="coinTableBody">
    </tbody>
  </table>
    <script>
        let coinList = $('#coinTableBody')
        fetch('https://api.coinpaprika.com/v1/ticker')
            .then( response => response.json() )
            .then( coins => {
                for (let i=0; i<50; i++) {
                    let coin = coins[i];
                    coinList.append(`
                      <tr>
                        <td>${coin.rank}</td>
                        <td>${coin.name}</td>
                        <td>${coin.symbol}</td>
                        <td>${coin.price_usd}</td>
                        <td>${coin.percent_change_1h}%</td>
                        <td>${coin.percent_change_24h}%</td>
                        <td>${coin.percent_change_7d}%</td>
                        <td>${coin.volume_24h_usd}</td>
                        <td>${coin.market_cap_usd}</td>
                      </tr>
                  `);
                }
            });
    </script>
</body>
</html>
2 Likes

Simple version, just javascript and html, single page, direct processing:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Exercise 3</title>
</head>
<body>

<h2>API Coins</h2>
<div id="coinTable"></div>

<script type="text/javascript">
	fetch('https://api.coinpaprika.com/v1/coins')
    .then( response => response.json() )
    .then( coins => {
    	let html = `<table><tr><th>Rank</th><th>Name</th><th>Symbol</th></tr>`;
        for (let i = 0; i < 20; ++i ) {
            const coin = coins[i];
            console.log( coin.name, coin.symbol );

            html += `<tr><td>${coin.rank}</td><td>${coin.name}</td><td>${coin.symbol}</td></tr>`;
        }
        html += `</table>`;
        document.getElementById('coinTable').innerHTML = html;
    } );
</script>

</body>
</html>

File in own map ‘exercise_lesson_3’ , named index.html
and accessed via own html server; python -m http.server
in browser accessible via: http://localhost.8000

1 Like

https://codepen.io/sbelka-1703/pen/PomGqgB

image

2 Likes

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Exercise</title>
    <h2>Top 20 coins/tokens from Coinpaprika</h2>
</head>
<body>
  <table>
    <thead>
      <th>#</th>
      <th>Coin Name</th>
      <th>Coin Symbol</th>
    </thead>
    <tbody id="coinInfo"></tbody>
  </table>
</body>
</html>

CSS:

body {
  max-width: max-content;
  margin: auto;
}

table {
    border-collapse: collapse;
    margin: 25px 0;
    font-size: 0.9em;
    font-family: sans-serif;
    min-width: 400px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
    text-align: center;  
}

table thead {
    background-color: #009879;
    color: #ffffff;
    text-align: center;
}

th, td {
    padding: 12px 15px;
}

tbody td {
    border-bottom: 1px solid #dddddd;
}

tbody tr:nth-of-type(even) {
    background-color: #f3f3f3;
}

tbody tr:last-of-type {
    border-bottom: 2px solid #009879;
}

tbody tr {
    font-weight: bold;
    color: #009879;
}

JS:

fetch('https://api.coinpaprika.com/v1/coins')
  .then( response => response.json() )
  .then( coins => {
  for (let i = 0; i < 20; ++i ) {
    const coin = coins[i];
    const coinRow = `
    <tr>
      <td>${coin.rank}</td>
      <td>${coin.name}</td>
      <td>${coin.symbol}</td>
    </tr>`;
    document.getElementById("coinInfo").innerHTML += coinRow;
  }
} );

image

2 Likes

Codepen

HTML

<title>Top 20 Coins</title>
<link href='https://fonts.googleapis.com/css?family=Abel' rel='stylesheet'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
 
<body>
   <h1>Top 20 Cryptocurrencies</h1>
      <div>
        <table>
            <thead>
               <tr>
                 <th>Rank</th>
                 <th>Name</th>
                 <th>Ticker</th>
               </tr>
            </thead>
   
            <tbody id = TableData>
            </tbody>
   
         </table>
      </div>
</body>


CSS


body {
  background-color: #000080;
  color: #FFF300
  
}
h1 {
  margin-left: 10%;
  text-align: center;
  font-size: 60px;
  font: Abel;
}

body {
  font: normal 20px Abel, sans-serif;
  text-align: center;
}

table {
  width: 60%;  
}

table, th, tr, td {
  margin-left: 25%;
  margin-right: auto;
  border: solid 1.6rem;
}

th {
  font-size:30px;
}

tr:hover {background-color: #FFF300; color: #000080;}


JavaScript

$(document).ready(function(){
  
  fetch('https://api.coinpaprika.com/v1/coins')

    .then( response => response.json() )

    .then( coins => {

        for (let i = 0; i < 20; ++i ) {

            const coin = coins[i];
            
            $("#TableData").append(
             `<tr>
               <td>${coin.rank}</td>
               <td>${coin.name}</td>
               <td>${coin.symbol}</td>
              </tr>`);
        }
    })  
});
1 Like

https://codepen.io/trentCodePen/pen/QWvqxJL

Here is the link for the CodePen

1 Like

Print Coin table Assignment.

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>


    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>  
    
   
    <link rel="stylesheet" href="app.css">
    <title>Document</title>
</head>
<body>
    

<h1 style="margin-bottom: 5px; text-align: center;">👋 MiPaprika</h1>
<br>
<h3 style="margin-bottom: 100px; text-align: center;">Top 20 Coins</h3>

<table class="table">
  <thead>
      <th style="color: orange">rank</th>
      <th style="color: orange">Name</th>
      <th style="color: orange">Symbol</th>
  </thead>
  <tbody id="coinData">
  </tbody>
</table>

<script src="app.js"></script>

</body>
        
</html>

Javascript

let coinsData = $('#coinData');

fetch('https://api.coinpaprika.com/v1/coins')
    .then( response => response.json() )
    .then( coins => {
        for (let i = 0; i < 20; ++i ) {
            const coin = coins[i];
          
        coinsData.append(`<tr>
       
        <td>  ${coin.rank}  </td>
        <td>  ${coin.name}  </td>
        <td>  ${coin.symbol} </td>
      </tr>  `)
        
        }

    } );

CSS

table {
    background-color: black;
    border: 1px solid white;
  }
  td{
    color: white;
  }```
1 Like
import './App.css';
import React from 'react';

class App extends React.Component {
  state = {};
  render() {
    return (
      <div className="App">
        <table>
          <thead>
            <tr>
              <th>ID</th>
              <th>Name</th>
              <th>Ticker</th>
            </tr>
          </thead>
          <tbody>
            {this.state.coinData}
          </tbody>
        </table>
      </div>
    );
  }

  componentDidMount(){
    this.generateCoinData();
  }

  generateCoinData = () => {
  fetch("https://api.coinpaprika.com/v1/coins")
      .then(response => response.json())
      .then((coins) => {
          var data = coins.map((coin) =>
          <tr><td>{coin.id}</td>
          <td>{coin.name}</td>
          <td>{coin.symbol}</td></tr>);
          this.setState({coinData: data.splice(0,20)});
      });
  }
}
export default App;

1 Like
const coinList = document.getElementById("CoinData");
fetch("https://api.coinpaprika.com/v1/coins")
  .then((response) => response.json())
  .then((coins) => {
    for (let i = 0; i < 20; ++i) {
      const coin = coins[i];
      let rows = `<tr>
                    <td>${coin.rank}</td>
                    <td>${coin.name}</td>
                    <td>${coin.symbol}</td>
                  </tr>`;
      coinList.innerHTML += rows;
    }
  });
  • HTML
<table id="tableList">
      <thead>
        <tr>
          <th>Rank</th>
          <th>Coin</th>
          <th>Symbol</th>
        </tr>
      </thead>
      <tbody id="CoinData"></tbody>
    </table>
1 Like

It took me a few days to figure this out :wink: But I did it, LOL

HTML

<title>Coinpaprika Exercise</title>
    <h2> Top 20 Coins</h2>
    <table id="tableList">
        
    </head>
<body>
    <tabable>
        <thead> 
            <th>Rank</th>
            <th>Coin</th>
            <th>Symbol</th>
            </thead>
        <tbody id="coinInfo"></tbody>

JS

fetch('https://api.coinpaprika.com/v1/coins')
  .then( response => response.json() )
  .then( coins => {
  for (let i = 0; i < 20; ++i ) {
    const coin = coins[i];
    const coinRow = `
    <tr>
      <td>${coin.rank}</td>
      <td>${coin.name}</td>
      <td>${coin.symbol}</td>
    </tr>`;
    document.getElementById("coinInfo").innerHTML += coinRow;
  }
} );
2 Likes

Click the preformatted text next time.
It’s the little </> button above the text box :slight_smile:

1 Like

codepen link https://codepen.io/greg-dubois/pen/MWoWdyx

1 Like