Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- РЕШЕНИЕ СЪС SWITCH:
- function darts(input) {
- let index = 0;
- let name = input[index++];
- let counterYes = 0;
- let counterNo = 0;
- let total = 301;
- while (true){
- let zone = input[index++];
- if (zone === "Retire") {
- break;
- }
- let dots = Number(input[index++]);
- switch (zone) {
- case "Double":
- dots *= 2;
- break;
- case "Triple":
- dots *= 3;
- break;
- }
- if (total < dots) {
- counterNo++;
- } else {
- total -= dots;
- counterYes++;
- }
- if (total === 0) {
- break;
- }
- }
- if (total === 0) {
- console.log(`${name} won the leg with ${counterYes} shots.`);
- } else {
- console.log(`${name} retired after ${counterNo} unsuccessful shots.`);
- }
- }
- РЕШЕНИЕ С IF-ELSE:
- function darts(input) {
- let index = 0;
- let name = input[index++];
- let counterYes = 0;
- let counterNo = 0;
- let total = 301;
- while (true){
- let zone = input[index++];
- if (zone === "Retire") {
- break;
- }
- let dots = Number(input[index++]);
- if (zone === "Double") {
- dots *= 2;
- } else if (zone === "Triple") {
- dots *= 3;
- }
- if (total < dots) {
- counterNo++;
- } else {
- total -= dots;
- counterYes++;
- }
- if (total === 0) {
- break;
- }
- }
- if (total === 0) {
- console.log(`${name} won the leg with ${counterYes} shots.`);
- } else {
- console.log(`${name} retired after ${counterNo} unsuccessful shots.`);
- }
- }
- РЕШЕНИЕ СЪС SHIFT() И ТЕРНАРЕН ОПЕРАТОР:
- function darts(input) {
- let name = input.shift();
- let counterYes = 0;
- let counterNo = 0;
- let total = 301;
- while (true) {
- let zone = input.shift();
- if (zone === "Retire") {
- break;
- }
- let dots = Number(input.shift()) * (zone === "Double" ? 2 : zone === "Triple" ? 3 : 1);
- _= total < dots ? counterNo++ : counterYes++;
- total -= total >= dots ? dots : 0;
- if (total === 0) {
- break;
- }
- }
- console.log( total === 0 ? `${name} won the leg with ${counterYes} shots.`
- : `${name} retired after ${counterNo} unsuccessful shots.`);
- }
Add Comment
Please, Sign In to add comment