planets.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env qjs
  2. "use strict"
  3. function Planet(name, mass, sunDist, hasRings) {
  4. var fields = ["name", "mass", "sunDist", "hasRings"] ;
  5. fields.forEach(el => {
  6. this[el] = eval(el) ;
  7. });
  8. this.toString = () => {
  9. return JSON.stringify(this) ;
  10. }
  11. }
  12. var planets = [
  13. new Planet("Mercury", 2.285e23, 5.8e10, false),
  14. new Planet("Venus", 4.867e24, 1.082e11, false),
  15. new Planet("Earth", 5.972e24, 1.495e11, false),
  16. new Planet("Mars", 6.4e23, 2.279e11, false),
  17. new Planet("Jupiter", 1.9e27, 7.78e11, true),
  18. new Planet("Saturn", 5.68e26, 1.427e12, true),
  19. new Planet("Uran", 8.69e25, 2.871e12, true),
  20. new Planet("Neptune", 1.02e26, 4.497e12, true),
  21. ]
  22. function sortArrayByField(arr, field, order=true) {
  23. var sortFunc = order ? (a, b) => {
  24. return a[field] > b[field] ? 1 : -1 ;
  25. } : (a, b) => {
  26. return a[field] < b[field] ? 1 : -1 ;
  27. }
  28. ;
  29. arr.sort(sortFunc);
  30. }
  31. function randomInt(max) {
  32. return Math.floor(Math.random() * max)
  33. }
  34. function printArray(arr, field=null) {
  35. if(field)
  36. arr.forEach((el) => {
  37. console.log(el[field])
  38. });
  39. else
  40. arr.forEach((el) => {
  41. console.log(el);
  42. });
  43. }
  44. function filteredArray(arr, filterFunc) {
  45. var ret = [];
  46. arr.forEach( (el) => {
  47. if(filterFunc(el))
  48. ret.push(el);
  49. });
  50. return ret
  51. }
  52. // 3
  53. function printPlanetsInOrderFromSun(arr){
  54. var cpy = arr.slice() ;
  55. sortArrayByField(cpy, "sunDist");
  56. printArray(cpy, "name");
  57. }
  58. // 4
  59. function printPlanetsWithoutRings(arr) {
  60. var cpy = filteredArray(arr, (el) =>{
  61. return !el.hasRings ;
  62. }) ;
  63. printArray(cpy, "name");
  64. }
  65. function findMax(arr, cmpFn){
  66. var ret = arr[0] ;
  67. arr.forEach((el) => {
  68. if(cmpFn(el, ret) == 1)
  69. ret = el;
  70. });
  71. return ret ;
  72. }
  73. function findByVal(arr, val, field) {
  74. var ret = null ;
  75. arr.some( (el) => {
  76. if(el[field] == val){
  77. ret = el;
  78. return true;
  79. }
  80. return false ;
  81. });
  82. return ret;
  83. }
  84. // 5
  85. function printPlanetsWithMinAndMaxMasses(arr) {
  86. var min = findMax(arr, (a, b) => {
  87. return a.mass < b.mass ? 1 : -1 ;
  88. }) ;
  89. var max = findMax(arr, (a, b) => {
  90. return a.mass > b.mass ? 1 : -1 ;
  91. }) ;
  92. console.log(min.name)
  93. console.log(max.name)
  94. }
  95. // 6
  96. function minAndMaxDistancesBetweenPlanets(a, b) {
  97. var ret = [] ;
  98. ret.push(Math.abs(a.sunDist - b.sunDist));
  99. ret.push(a.sunDist + b.sunDist);
  100. return ret ;
  101. }
  102. // 7
  103. function theMostDistancedPlanetAstronautCanReach(ps, fuelDist) {
  104. var earth = findByVal(ps, "Earth", "name")
  105. var cpy = planets.slice()
  106. sortArrayByField(cpy, "sunDist", false);
  107. var reachablePlanet = null ;
  108. cpy.some((el)=>{
  109. var [min, max] = minAndMaxDistancesBetweenPlanets(earth, el) ;
  110. if(min <= fuelDist ){
  111. reachablePlanet = el ;
  112. return true ;
  113. }
  114. return false ;
  115. });
  116. if(reachablePlanet != null && reachablePlanet != earth){
  117. console.log(`The most distanced planet we can reach with ${fuelDist} is ${reachablePlanet.name}`);
  118. } else {
  119. console.log(`We cannot reach any planet with ${fuelDist}`);
  120. }
  121. }
  122. console.log("Planets in order from the sun:")
  123. printPlanetsInOrderFromSun(planets);
  124. console.log();
  125. console.log("Planets without rings:")
  126. printPlanetsWithoutRings(planets);
  127. console.log();
  128. console.log("Planets with min and max masses:");
  129. printPlanetsWithMinAndMaxMasses(planets);
  130. console.log();
  131. var [a, b] = [planets[randomInt(planets.length-1)], planets[randomInt(planets.length-1)]] ;
  132. var [min, max] = minAndMaxDistancesBetweenPlanets(a, b);
  133. console.log(`Min and max distances between ${a.name} and ${b.name} are ${min} and ${max}`);
  134. console.log()
  135. theMostDistancedPlanetAstronautCanReach(planets, 4e10)
  136. theMostDistancedPlanetAstronautCanReach(planets, 1.5e11)
  137. theMostDistancedPlanetAstronautCanReach(planets, 1.6e12)