You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.4 KiB

6 years ago
  1. toastr.options = {
  2. "positionClass": "toast-bottom-right"
  3. };
  4. var priceData = "";
  5. //get the dataset
  6. $(document).ready(function() {
  7. $.ajax({
  8. type: "GET",
  9. url: "http://api.fairplayground.info/rawdata/faircoin_prices.csv",
  10. dataType: "text",
  11. success: function(data) {processData(data);}
  12. });
  13. });
  14. function csvToArray(csv) {
  15. var a1 = csv.split("\n");
  16. var r = [];
  17. for(var i=0; i<a1.length; i++) {
  18. var a2 = a1[i].split(",");
  19. r.push(a2);
  20. }
  21. return(r);
  22. }
  23. function processData(data) {
  24. priceData = csvToArray(data);
  25. };
  26. function calculateEuros() {
  27. document.getElementById("result").innerHTML="";
  28. var faircoins = document.getElementById("inputFaircoin").value;
  29. var date = document.getElementById("inputDate").value;
  30. if(!faircoins){
  31. toastr.warning("please, fill the faircoin input box");
  32. return;
  33. }
  34. if(!date){
  35. toastr.warning("no date");
  36. return;
  37. }
  38. //let's find the date in the priceData
  39. var selectedPriceData = []
  40. for(var i=0; i<priceData.length; i++) {
  41. if(priceData[i][0]==date) {
  42. selectedPriceData = priceData[i];
  43. }
  44. }
  45. if(selectedPriceData.length<1) {
  46. toastr.warning("selected date not found in the dataset");
  47. return;
  48. }
  49. console.log(selectedPriceData);
  50. var euros = faircoins * selectedPriceData[3];
  51. document.getElementById("inputEuro").value= euros;
  52. document.getElementById("result").innerHTML = faircoins + "FC, are " + euros + "€";
  53. toastr.success(faircoins + "FC, are " + euros + "€");
  54. }
  55. function calculateFaircoins() {
  56. document.getElementById("result").innerHTML="";
  57. var euros = document.getElementById("inputEuro").value;
  58. var date = document.getElementById("inputDate").value;
  59. if(!euros){
  60. toastr.warning("please, fill the euro input box");
  61. return;
  62. }
  63. if(!date){
  64. toastr.warning("no date");
  65. return;
  66. }
  67. //let's find the date in the priceData
  68. var selectedPriceData = []
  69. for(var i=0; i<priceData.length; i++) {
  70. if(priceData[i][0]==date) {
  71. selectedPriceData = priceData[i];
  72. }
  73. }
  74. if(selectedPriceData.length<1) {
  75. toastr.warning("selected date not found in the dataset");
  76. return;
  77. }
  78. console.log(selectedPriceData);
  79. var faircoins = euros / selectedPriceData[3];
  80. document.getElementById("inputFaircoin").value= faircoins;
  81. document.getElementById("result").innerHTML = euros + "€, are " + faircoins + "FC";
  82. toastr.success(euros + "€, are " + faircoins + "FC");
  83. }