Advertisement
plirof2

gpt : csv elo check v001

Apr 24th, 2024
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I have a csv file with contents like this :
  2. playername,id,grelo,fidestd,fiderapid,fideblitz
  3. Jack Daniels,53365,0,0,0,0
  4. Super Caotonic,54257,1046,1617,1596,1626
  5. Amola kaloyumba,57854,0,0,0,0
  6. Hello world,0,0,0,0,0
  7.  
  8. I want a function in javascript that will load this file once and create an array with it's contents.
  9. I want a second JS function named checkNameRelation(name) that will check if the string name contains any of the words (seperated by space) contained in playername or id of the csv. If yes then return a string in the follwing format :  playername <a href="chessfed.gr/elo/id>id</a>gr:grelo |Fide s:fidestd,r:fiderapid,b:fideblitz
  10. ----------------------------
  11.  
  12. Here is a sample code that achieves the requirements mentioned:
  13.  
  14. First, we will create a function to load the CSV file and create an array with its contents:
  15.  
  16. ```javascript
  17. let csvData = [];
  18.  
  19. function loadCSV(file) {
  20.    fetch(file)
  21.        .then(response => response.text())
  22.        .then(data => {
  23.            csvData = data.split('\n').map(row => row.split(','));
  24.            console.log(csvData);
  25.        });
  26. }
  27.  
  28. loadCSV('data.csv');
  29. ```
  30.  
  31. Next, we will create the `checkNameRelation` function to check if the input `name` matches any playername or id in the CSV:
  32.  
  33. ```javascript
  34. function checkNameRelation(name) {
  35.    for (let i = 1; i < csvData.length; i++) {
  36.        if (csvData[i][0].includes(name) || csvData[i][1].includes(name)) {
  37.            return `${csvData[i][0]} <a href="chessfed.gr/elo/${csvData[i][1]}">${csvData[i][1]}</a> gr:${csvData[i][2]} | Fide s:${csvData[i][3]}, r:${csvData[i][4]}, b:${csvData[i][5]}`;
  38.        }
  39.    }
  40.    return 'No match found.';
  41. }
  42.  
  43. console.log(checkNameRelation('ΑΠΟΣΤΟΛΟΣ')); // Example usage
  44. ```
  45.  
  46. Ensure that you replace `'data.csv'` with the correct path to your CSV file.
  47.  
  48. You can now use the `checkNameRelation` function to check if a given name matches any playername or id in the CSV and get the formatted output as described.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement