Advertisement
pacho_the_python

task_1

Apr 20th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. function horse_racing(data) {
  2. let horse_positions = data.shift().split('|')
  3. data.pop()
  4. for (const line of data) {
  5. let command_data = line.split(' ')
  6. let command = command_data[0]
  7. if (command === 'Retake') {
  8. let overtaking_horse = command_data[1]
  9. let overtaken_horse = command_data[2]
  10. let overtaking_index = horse_positions.indexOf(overtaking_horse)
  11. let overtaken_index = horse_positions.indexOf(overtaken_horse);
  12. [horse_positions[overtaking_index], horse_positions[overtaken_index]] = [horse_positions[overtaken_index], horse_positions[overtaking_index]]
  13. console.log(`${overtaking_horse} retakes ${overtaken_horse}.`)
  14. }
  15. else if (command === 'Trouble') {
  16. let trouble_horse = command_data[1]
  17.  
  18. if (horse_positions.indexOf(trouble_horse) !== 0) {
  19. let horse_index = horse_positions.indexOf(trouble_horse);
  20. [horse_positions[horse_index], horse_positions[horse_index + 1]] = [horse_positions[horse_index + 1], horse_positions[horse_index]]
  21. console.log(`Trouble for ${trouble_horse} - drops one position.`)
  22. }
  23. }
  24. else if (command === 'Rage') {
  25. let rage_horse = command_data[1]
  26. let horse_index = horse_positions.indexOf(rage_horse)
  27. if (horse_index === horse_positions[horse_positions.length - 2]) {
  28. let current_horse = horse_positions.splice(horse_index, 1)
  29. horse_positions.push(current_horse);
  30. } else if (horse_index < 1) {
  31. [horse_positions[horse_index], horse_positions[horse_index - 2]] = [horse_positions[horse_index - 2], horse_positions[horse_index]]
  32. }
  33. console.log(`${rage_horse} rages 2 positions ahead.`)
  34. }
  35. else if (command === 'Miracle') {
  36. let last_horse = horse_positions.shift()
  37. horse_positions.push(last_horse)
  38. console.log(`What a miracle - ${last_horse} becomes first.`)
  39. }
  40. }
  41. console.log(horse_positions.join('->'))
  42. console.log(`The winner is: ${horse_positions[horse_positions.length - 1]}`)
  43. }
  44.  
  45. horse_racing(['Onyx|Domino|Sugar|Fiona',
  46. 'Trouble Onyx',
  47. 'Retake Onyx Sugar',
  48. 'Rage Domino',
  49. 'Miracle',
  50. 'Finish'])
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement