Im learning JS and have set myself the challenge to re-create a football league table, from an array of match results.
Everything is going really well and I'm almost complete, but I can't filter my 'FORM GUIDE' array to their corresponding Letters.
I have the points scored over the last 5 games ONLY outputted as an array (below);
[3, 1, 3, 0, 3]
But I'd like to output this as 3 = W, 1 = D, 0 = L.
So... W, D, W, L, W
Can someone please explain how I can do this?
Thank you
'use strict';
// Tottenham's Premier League scores & points 21-22 //
//--------------------------------------------------//
const scoresPoints = [
[1, 0, 3], // Watford
[1, 1, 1], // Southampton
[3, 0, 3], // Crystal Palace
[2, 2, 1], // Liverpool
[3, 0, 3], // Norwich
[2, 0, 3], // Brentford
[2, 1, 3], // Leeds
[0, 0, 1], // Everton
[3, 2, 3], // Newcastle
[0, 3, 0], // Man U
[0, 1, 0], // West Ham
[2, 1, 3], // Villa
[1, 3, 0], // Arsenal
[0, 3, 0], // Chelsea
[0, 3, 0], // Crystal Palace
[1, 0, 3], // Watford
[1, 0, 3], // Wolves
[1, 0, 3], // Man City
];
// Define The functions & arrays--------------------//
//--------------------------------------------------//
let tottenhamScores;
let totalTottenhamGoals;
let totalTottenhamPoints;
let totalOpponentsGoals;
let tottenhamForm = [];
// The goals scored by Tottenham
const tottenhamGoals = [];
// The points scored by Tottenham
const tottenhamPoints = [];
// The goals scored by the opponents
const opponentsGoals = [];
// Filter the data from each array------------------//
//--------------------------------------------------//
for (let i = 0; i < scoresPoints.length; i ) {
tottenhamScores = scoresPoints[i][0];
// Just Tottenham's goals
tottenhamGoals.push(tottenhamScores);
// Just Tottenham's points
const leaguePoints = scoresPoints[i][2];
tottenhamPoints.push(leaguePoints);
// Just the opponents goals
const opponentsScores = scoresPoints[i][1];
opponentsGoals.push(opponentsScores);
// Just Tottenham's Form
const leagueForm = scoresPoints[i][2];
tottenhamForm.push(leagueForm);
}
// Adding up the arrays-----------------------------//
//--------------------------------------------------//
// Adding up Tottenham's goals
for (let i = 0; i < tottenhamGoals.length; i ) {
totalTottenhamGoals = tottenhamGoals.reduce(function (a, b) {
return a b;
}, 0);
}
// Adding up Tottenham's points
for (let i = 0; i < tottenhamPoints.length; i ) {
totalTottenhamPoints = tottenhamPoints.reduce(function (a, b) {
return a b;
}, 0);
}
// Adding up the opponents goals
for (let i = 0; i < opponentsGoals.length; i ) {
totalOpponentsGoals = opponentsGoals.reduce(function (a, b) {
return a b;
}, 0);
}
// Last 5 games-------------------------------------//
//--------------------------------------------------//
// Find the individual values
function occurrence(pointValues, value) {
return pointValues.filter(v => v === value).length;
}
const win = occurrence(tottenhamPoints, 3);
const draw = occurrence(tottenhamPoints, 1);
const loss = occurrence(tottenhamPoints, 0);
// Filter to last five games
function lastFiveGames(form, five) {
form = tottenhamForm.slice(0, five);
return form;
}
const latestForm = lastFiveGames(tottenhamForm, 5);
// Convert points to represented letters
const letteredResult = latestForm.map(result => {
switch (result) {
case 0:
return 'L';
case 1:
return 'D';
case 3:
return 'W';
default:
return 'No Form To Display';
}
});
// Print the statement & table----------------------//
//--------------------------------------------------//
console.log(
`SUMMARY
--------
--------
Throughout the 2021-22 Premier League season, Tottenham Hotspur have scorred ${totalTottenhamGoals} goals and conceeded ${totalOpponentsGoals}.
This has gained Tottenham Hotspur ${totalTottenhamPoints} points to date.
(Dropping ${
scoresPoints.length * 3 - totalTottenhamPoints
} points throughout the season from the maximum of ${
scoresPoints.length * 3
} available).
TABLE & FORM GUIDE ---
----------------------
${scoresPoints.length} Played
${win} Wins
${draw} Draws
${loss} Losses
${totalTottenhamGoals} For
${totalOpponentsGoals} Against
${totalTottenhamGoals - totalOpponentsGoals} Goal Difference
${totalTottenhamPoints} POINTS IN TOTAL
FORM (Last 5 Games) ${letteredResult}
----------------------
----------------------`
);
CodePudding user response:
You can use map:
[3, 1, 3, 0, 3].map(e => { if (e == 3) return "W"; if (e == 1) return "D"; if (e == 0) return "L"; throw `Unexpected value: ${e}`; })
CodePudding user response:
let arr = [3, 1, 3, 0, 3];
let arrMap = arr.map((i) => {
if (i == 3) return "W";
if (i == 0) return "L";
return "D"
})
console.log(arrMap)
CodePudding user response:
You can achieve this with Array.map
and a nice switch
statement. Try this way:
const arr = [3, 1, 3, 0, 3]
const matched = arr.map(result => {
switch(result){
case 0:
return 'L'
case 1:
return 'D'
case 3:
return 'W'
default:
return 'X'
}
})
console.log(matched)
CodePudding user response:
You can achieve this clean and simple using map
and an object acting as Enum where the key will represent the points and the value will represent the result char.
const array = [3, 1, 3, 0, 3];
const cases = {0: 'L', 1: 'D', 3: 'W'};
const matches = array.map(e => cases[e]);
console.log(matches)
More about map
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
CodePudding user response:
Instead of using map
, you could also use forEach
. The main difference is that map
will return a new array, whereas forEach
doesn't return anything and can be used to change the original array. Here is how:
const logic = {
3: 'W',
1: 'D',
0: 'L'
}
const data = [3, 1, 3, 0, 3];
data.forEach((value, key) => data[key] = logic[value]);
console.log(data);
CodePudding user response:
const arr = [3, 1, 3, 0, 3, 2, 4];
const data = arr.map(v => ['L','D','_','W'][v] || '_');
console.log( data );