Im new here and into programming. I
m not native english so please apologize if you dont understand me very well :-)
I`ve started learning Javascript and still doing. Now I want to achieve a simple project which gives me two Teams with equal skills.
Questions:
- It seems to work but I want to get random teams. This gives me always the same. How can I resolve this?
- I am able to print the values to see that its correct, but how can I print the player names (Variables like P1, P2, P3...)
Thank you
//Player skill values
let P1 = 2;
let P2 = 3;
let P3 = 1;
let P4 = 3;
let P5 = 4;
let P6 = 4;
let P7 = 5;
let P8 = 2;
//try to achieve two equal teams
function aufteilen(arr) {
arr.sort().reverse()
let a = [], b = [], sumA = 0, sumB = 0, i = 0
while (i < arr.length) {
if (!sumA && !sumB || sumA == sumB) {
a.push(arr[i])
sumA += arr[i]
} else if (sumA < sumB) {
a.push(arr[i])
sumA += arr[i];
} else if (sumB < sumA) {
b.push(arr[i])
sumB += arr[i];
}
i++
}
console.log(`Total: ${sumA} ${sumB}`)
return [a, b]
}
console.log(aufteilen([P1, P2, P3, P4, P5, P6, P7, P8]))