0

First I need to create an array with the player's name taken from the input #P1 and #P2 and #P3.

Example: Using members[class] look up standing[code] and place standing[rank] into an added numeric field in the array.

Sort the array by [rank] and [name].

Output a delimited file called "players.txt" ---> "Paul|Jim|Bob" THIS PART IS MISSING FROM THE JS BELOW. HOW CAN I WRITE TO PLAYERS.TXT ?

HTML: (the html code with the player's name [#P1] and class [Y1])

<div class="block">
<div class="line"><input type = "text" id = "P1" name = "N1" /></div>
<div class="line"><input type = "text" id="Y1"></div>
</div> <br />

<div class="block">
<div class="line"><input type = "text" id = "P2" name = "N2" /></div>
<div class="line"><input type = "text" id="Y2"></div>
</div> <br />

<div class="block">
<div class="line"><input type = "text" id = "P3" name = "N3" />
  <div class="line"><input type = "text" id="Y3"></div>
</div> <br />

JSON:

<script>
var members = [
{"Class": "A", "Name": "Paul"},
{"Class": "C", "Name": "Jim"},
{"Class": "B", "Name": "Bob"},
];
</script>

<script>
var standing = [
{"code" : "A", "rank" :  "1"},
{"code" : "B", "rank" :  "2"},
{"code" : "C", "rank" :  "3"},
];
</script>

JS:

var memberstosort = [], sorted = [];

function sort() {
memberstosort.sort(function(l, r) { return l.rank - r.rank});
Object.keys(memberstosort).map(function(objectKey, index) {
var value = memberstosort[objectKey].name;
sorted.push(value);

});
sorted = sorted.join('|');
document.getElementById('result').innerHTML = sorted;
}
var currentname, currentclass, index, currentrank;

function fillClass(id) {
currentname = document.getElementById(id).value;
index = members.findIndex(checkClass);
currentclass = members.filter(checkClass).length ? 
members.filter(checkClass)[0].Class : null;
if (currentclass) {
currentrank = standing.filter(checkRank).length ? 
standing.filter(checkRank)[0].rank : null;
} else {
return;
}
var currentmember = {};
currentmember.name = currentname;
currentmember.rank = currentrank;
memberstosort.push(currentmember);
}

function checkClass(player) {
return player.Name.toLowerCase() === currentname.toLowerCase();
}

function checkRank(standing) {
return standing.code === currentclass;
}
verlager
  • 794
  • 5
  • 25
  • 43
  • 1
    So what is the issue you facing? – ssilas777 Mar 27 '18 at 03:24
  • I am unable to even pick the correct tools ... php or javascript or type of array (associated?) or json for generating the "player.txt". I am in a world of general bewilderment. – verlager Mar 27 '18 at 03:27
  • Considering everything you're doing is client-side, it makes little sense to use PHP. And in terms of worlds, you'll find Earth more realistic. – Obsidian Age Mar 27 '18 at 03:30
  • What this program is supposed to do is make tournament pairings by grouping players ranked similarly. The actual program is at http://verlager.com/account-pure.php (press the green "+" after editing a few names with auto-complete) – verlager Mar 27 '18 at 03:33
  • Did you try something? Give your approach here. I dont really understand the purpose of this ... – vicnoob Mar 27 '18 at 03:39
  • As I say... I am *completely* clueless. I have no chance of doing this. It's for our chess club and I am dependent on the kindness of strangers and the forbearance of reptiles. – verlager Mar 27 '18 at 03:42
  • So first you want Enter a Name then class Y is automatically determined, right? – vicnoob Mar 27 '18 at 03:55
  • Yes, we have the code for that. If you enter a few names using the auto-complete, the class code will typically show up. Then press the green + sign and the pairing page shows up... but the players' names are not sorted! – verlager Mar 27 '18 at 04:00
  • Same class same rank? Or sorted by what – vicnoob Mar 27 '18 at 04:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167593/discussion-between-vicnoob-and-verlager). – vicnoob Mar 27 '18 at 04:22
  • Yes, I guess. See I need to sort by rank, using members[class] to look up standing[code] and return standing[rank] to the array. – verlager Mar 27 '18 at 04:23

1 Answers1

-1

Hope this will help you. I am not sure because I dont really under

var members = [{
    "Class": "A",
    "Name": "Paul"
  },
  {
    "Class": "C",
    "Name": "Jim"
  },
  {
    "Class": "B",
    "Name": "Bob"
  },
];

var standing = [{
    "code": "A",
    "rank": "1"
  },
  {
    "code": "B",
    "rank": "2"
  },
  {
    "code": "C",
    "rank": "3"
  },
];
var memberstosort = [], sorted = [];

function sort() {
  memberstosort.sort(function(l, r) { return l.rank - r.rank});
    Object.keys(memberstosort).map(function(objectKey, index) {
    var value = memberstosort[objectKey].name;
    sorted.push(value);

    });
    sorted = sorted.join('|');
    document.getElementById('result').innerHTML = sorted;
}
var currentname, currentclass, index, currentrank;

function fillClass(id) {
  currentname = document.getElementById(id).value;
  index = members.findIndex(checkClass);
  currentclass = members.filter(checkClass).length ? members.filter(checkClass)[0].Class : null;
  if (currentclass) {
    currentrank = standing.filter(checkRank).length ? standing.filter(checkRank)[0].rank : null;
  } else {
    return;
  }
  var currentmember = {};
  currentmember.name = currentname;
  currentmember.rank = currentrank;
  memberstosort.push(currentmember);
}

function checkClass(player) {
  return player.Name.toLowerCase() === currentname.toLowerCase();
}

function checkRank(standing) {
  return standing.code === currentclass;
}

HTML:

<div class="block" id="player1">
  <div class="line">Player 1<input type="text" id="P1" name="N1" onblur="fillClass(this.id)" /></div>
  <div class="line"> Class <input type="text" id="Y1"></div>
</div> <br />

<div class="block">
  <div class="line">Player 2<input type="text" id="P2" name="N2" onblur="fillClass(this.id)"/></div>
  <div class="line">Class<input type="text" id="Y2"></div>
</div> <br />

<div class="block">
  <div class="line">Player 3<input type="text" id="P3" name="N3" onblur="fillClass(this.id)"/>
    <div class="line">Class<input type="text" id="Y3"></div>
  </div> <br />
<div id="result">

</div>
  <button onclick="sort()">
Check
</button>
vicnoob
  • 1,169
  • 13
  • 27
  • Darn... didn't work. I added your code to http://verlager.com/account-pure.php but it didn't work; the names in super-dev.php are still unsorted. Any idea what's wrong? – verlager Mar 27 '18 at 05:20
  • Can you try it on jsfiddle or codepen? If it works, try to modify it to fit your project. – vicnoob Mar 27 '18 at 06:02
  • I created https://jsfiddle.net/1a53sh1c/1/ but it doesn't display results. – verlager Mar 27 '18 at 06:19
  • @verlager change loadtype to no wrap - in and retry – vicnoob Mar 27 '18 at 06:23
  • Any chance you could show me how to write "Paul|Jim|Bob" to "players.txt" so that the super-dev.php could read it? – verlager Mar 27 '18 at 06:44
  • https://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript It should help you – vicnoob Mar 27 '18 at 06:48
  • Or you may send the result to your back-end part and let it create a file – vicnoob Mar 27 '18 at 06:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167601/discussion-between-verlager-and-vicnoob). – verlager Mar 27 '18 at 07:14