1

Trying to pull JSON values from here: https://covid-19api.com/api/all-today

However, the first item is cast as a string as opposed to an int. This is my testing code so far, I'm not sure how to cast the item as an int rather than a string.

var requestOptions = {
  method: 'GET',
  redirect: 'follow'
};

fetch("https://covid-19api.com/api/all-today", requestOptions)
  .then(response => response.json())
  .then(result => {
    let nconf = result.confirmed;
    document.getElementById('nconf').innerHTML = nconf.toLocaleString('en');
    
    let ndeath = result.deaths;
    document.getElementById('ndeath').innerHTML = ndeath.toLocaleString('en');
    
    let nrecov = result.recovered;
    document.getElementById('nrecov').innerHTML = nrecov.toLocaleString('en');
  })
  .catch(error => console.log('error', error));
evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
Nathan Brown
  • 29
  • 1
  • 7
  • 1
    `let nrecov = parseInt(result.recovered)` ? – ABGR Jun 26 '20 at 17:59
  • 1
    +1 to @ABGR. I'd just add the radix (the mathematical base and second param of parseInt) `let nrecov = parseInt(result.recovered, 10)` – jrnxf Jun 26 '20 at 18:01
  • @ABGR don't forget the radix – evolutionxbox Jun 26 '20 at 18:02
  • Damn I love this platform thank you guys. Can't believe it was that simple – Nathan Brown Jun 26 '20 at 18:05
  • @evolutionxbox that's optional I guess. However, it's good to add I think – ABGR Jun 26 '20 at 18:06
  • 1
    try `let nconf = +result.confirmed;` – Mario Jun 26 '20 at 18:06
  • @NathanBrown just on another note, why do you actually want to cast into `int`, String would do just as well. – ABGR Jun 26 '20 at 18:12
  • @Mario woah cool - is there any downside of that versus parseInt? – jrnxf Jun 26 '20 at 18:12
  • 1
    @ABGR My guess is so he can leverage Number.prototype.toLocaleString() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString – jrnxf Jun 26 '20 at 18:14
  • @coloradocolby yup that makes sence – ABGR Jun 26 '20 at 18:16
  • Cause the rest of the data is in INT and it automatically adds commas for values in thousands, where as a string is just a bunch of numbers, so it wasn't aesthetically pleasing @ABGR – Nathan Brown Jun 26 '20 at 18:47
  • about Numeric conversion usnig unary +, perhaps what you should take into account is that parseInt will try to obtain an integer so that if you pass as argument `"1.5"` it will get `1` and its passing `"1aaa5"` will return `1`, while unary + when trying to convert `"1.5"` will return `1.5` and in the case of `"1aaa5"` `NaN` will return, in your case `result.confirmed` will be an integer, so it is indistinct which method to use, only it is much shorter to write. Finally using unary + is the same as using Number ("1.5") – Mario Jun 26 '20 at 19:02
  • I think the answer to your question is here https://stackoverflow.com/questions/1133770/how-to-convert-a-string-to-an-integer-in-javascript, – Mario Jun 26 '20 at 19:06

2 Answers2

0

You can just use parseInt to cast the string into int

 let nrecov = parseInt(result.recovered, 10)
ABGR
  • 4,631
  • 4
  • 27
  • 49
0

Here's a complete solution. Seems odd that the API returns deaths and recovered as integers but not confirmed lol ¯|_(ツ)_/¯

<!DOCTYPE html>
<html lang="en">
  <body>
    <p id="nconf"></p>
    <p id="ndeath"></p>
    <p id="nrecov"></p>
  </body>
  <script>
    var requestOptions = {
      method: "GET",
      redirect: "follow",
    };

    fetch("https://covid-19api.com/api/all-today", requestOptions)
      .then((response) => response.json())
      .then((result) => {
        console.log("result", result);
        let nconf = parseInt(result.confirmed, 10);
        document.getElementById("nconf").innerHTML = nconf.toLocaleString("en");

        let ndeath = result.deaths;
        document.getElementById("ndeath").innerHTML = ndeath.toLocaleString(
          "en"
        );

        let nrecov = result.recovered;
        document.getElementById("nrecov").innerHTML = nrecov.toLocaleString(
          "en"
        );
      })
      .catch((error) => console.log("error", error));
  </script>
</html>
jrnxf
  • 744
  • 8
  • 22