Advertisement
Guest User

JS Advanced Exam - 19 December 2018

a guest
Jun 5th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. function dart() {
  2.  
  3. $(`#firstLayer`).on(`click`, onShootAction)
  4.  
  5.  
  6. let dictionary = {
  7. firstLayer: 1,
  8. secondLayer: 2,
  9. thirdLayer: 3,
  10. fourthLayer: 4,
  11. fifthLayer: 5,
  12. sixthLayer: 6
  13. }
  14.  
  15. let isHome = true;
  16. let $firstPlayerScore = $(`#home p:nth-child(1)`);
  17. let $secondPLayerScore = $(`#away p:nth-child(1)`);
  18.  
  19. let $scoreboard = $(`#scoreBoard table tbody`);
  20.  
  21. let $turns = $(`#turns`);
  22. let $firstTurn = $turns.children(`:nth-child(1)`);
  23. let $secondTurn = $turns.children(`:nth-child(2)`);
  24.  
  25. function onShootAction(e) {
  26. let currentValue = getValue(e);
  27. let $currentScore;
  28.  
  29. if (isHome) {
  30. $currentScore = Number($firstPlayerScore.text()) + currentValue;
  31. $firstPlayerScore.text($currentScore);
  32.  
  33. } else {
  34. $currentScore = Number($secondPLayerScore.text()) + currentValue;
  35. $secondPLayerScore.text($currentScore);
  36.  
  37. }
  38.  
  39.  
  40. checkForWinner($currentScore);
  41. isHome = !isHome;
  42. switchTurns(isHome);
  43.  
  44. }
  45.  
  46. function getValue(e) {
  47. let currentIdOfDiv = e.target.id;
  48. let indexOfTr = dictionary[currentIdOfDiv];
  49. let currentTr = $scoreboard.children(`:nth-child(${indexOfTr})`);
  50. let value = Number(currentTr.children(`:nth-child(2)`).text()
  51. .split(` `)[0]);
  52. return value;
  53. }
  54.  
  55. function checkForWinner(currentScore) {
  56.  
  57. if (currentScore >= 100) {
  58.  
  59. let $home = $(`#Home`).children(`:nth-child(2)`)
  60. let $away = $(`#Away`).children(`:nth-child(2)`)
  61.  
  62.  
  63. $(`#firstLayer`).off(`click`)
  64.  
  65. if (isHome) { // home is winning
  66. $home.css(`background`, `green`)
  67. $away.css(`background`, `red`)
  68. } else { //away is winning
  69. $away.css(`background`, `green`)
  70. $home.css(`background`, `red`)
  71.  
  72. }
  73.  
  74. // just for testing...
  75. // let homeScoree = $("#Home").find("p").text();
  76. // let awayScoreee = $("#Away").find("p").text();
  77. // console.log(homeScoree);
  78. // console.log(awayScoreee);
  79. }
  80. }
  81.  
  82. function switchTurns(isHome) {
  83. //mistake here with the turns... made it home/away.. :(
  84. //mistake => had to check the css , on how to change the multiple css selectors
  85. let turnOn = `Turn on `
  86. let nextIs = `Next is `
  87. let home = `Home`;
  88. let away = `Away`;
  89.  
  90. if (isHome) {
  91.  
  92. $firstTurn.text(turnOn + home);
  93. $secondTurn.text(nextIs + away);
  94. // $firstTurn.css({ "textDecoration": "underline", "fontWeight": "bold" })
  95. // $secondTurn.css({ "textDecoration": "none", "fontWeight": "normal" })
  96. } else {
  97.  
  98. $secondTurn.text(nextIs + home);
  99. $firstTurn.text(turnOn + away);
  100. // $firstTurn.css({ "textDecoration": "none", "fontWeight": "normal" })
  101. // $secondTurn.css({ "textDecoration": "underline", "fontWeight": "bold" })
  102. }
  103.  
  104.  
  105. }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement