Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function numberWars (input) {
- let index = 0;
- let nameOf1stPlayer = input[index];
- index++;
- let nameOf2ndPlayer = input[index];
- index++;
- let command = input[index];
- let result1st = 0;
- let result2nd = 0;
- while(command !== "End of game") {
- let cardOf1st = Number(input[index]);
- index++;
- let cardOf2nd = Number(input[index]);
- index++;
- if (cardOf1st > cardOf2nd) {
- result1st += cardOf1st - cardOf2nd;
- } else if (cardOf2nd > cardOf1st) {
- result2nd += cardOf2nd - cardOf1st;
- } else if (cardOf1st === cardOf2nd) {
- console.log("Number wars!");
- cardOf1st = Number(input[index]);
- index++;
- cardOf2nd = Number(input[index]);
- if (cardOf1st > cardOf2nd) {
- console.log(`${nameOf1stPlayer} is winner with ${result1st} points`);
- } else if (cardOf2nd > cardOf1st) {
- console.log(`${nameOf2ndPlayer} is winner with ${result2nd} points`);
- }
- break;
- }
- command = input[index];
- }
- if (command === "End of game") {
- console.log(`${nameOf1stPlayer} has ${result1st} points`);
- console.log(`${nameOf2ndPlayer} has ${result2nd} points`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement