script2.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. function truncateFraction (number) {
  2. try {
  3. let strNum = String(number).split('.')
  4. if(strNum[1].length > 2) {
  5. strNum[1] = strNum[1].slice(0, 2)
  6. }
  7. return Number(strNum.join('.'))
  8. } catch (e) {
  9. return number
  10. }
  11. }
  12. var loop = true
  13. while(loop) {
  14. switch(prompt('input task name:', '')) {
  15. case 'switch: sizes':
  16. let usSize = [
  17. {
  18. number: [6],
  19. men: 'S',
  20. woman:['XXS']
  21. },
  22. {
  23. number: [8, 10],
  24. men: 'M',
  25. woman:['XXS', 'XS']
  26. },
  27. {
  28. number: [12, 14],
  29. men: 'L',
  30. woman:['S', 'M']
  31. },
  32. {
  33. number: [16, 18],
  34. men: 'XL',
  35. woman:['L','XL']
  36. },
  37. {
  38. number: [20, 22],
  39. men: 'XXL',
  40. woman:['XXL', 'XXXL']
  41. },
  42. ]
  43. let rusSize = prompt('Введите НАШ формат размера для переводa на на Американский формат','')
  44. console.log(isNaN(rusSize))
  45. switch(!isNaN(rusSize) && +rusSize >= 40 && +rusSize <= 56) {
  46. case true:
  47. rusSize = +rusSize
  48. let flag = 0
  49. switch(rusSize % 2 !== 0) {
  50. case true:
  51. rusSize--
  52. flag++
  53. }
  54. for(let i = 0; i < usSize.length; i++) {
  55. let temp = usSize[i].number.findIndex(element => { return element === (rusSize - 34)})
  56. switch( temp >= 0) {
  57. case true:
  58. alert(`
  59. ${rusSize+flag}-й размер в американском формате:
  60. +---------------------------------------+
  61. Размер: ${usSize[i].number[temp]}
  62. Мужчины: ${usSize[i].men}
  63. Женщины: ${usSize[i].woman[temp]}
  64. +---------------------------------------+
  65. `)
  66. i = usSize.length
  67. break;
  68. default:
  69. continue;
  70. }
  71. }
  72. break;
  73. case false:
  74. alert('Неправильный размер.')
  75. break;
  76. }
  77. break;
  78. case 'switch: if':
  79. let color = prompt("Введите цвет","");
  80. if(color === 'red') {
  81. document.write("<div style='background-color: red;'>красный</div>");
  82. document.write("<div style='background-color: black; color: white;'>черный</div>");
  83. } else if(color === 'blue') {
  84. document.write("<div style='background-color: blue;'>синий</div>");
  85. document.write("<div style='background-color: green;'>зеленый</div>");
  86. } else if(color === 'green') {
  87. document.write("<div style='background-color: green;'>зеленый</div>");
  88. } else if(color === 'black') {
  89. document.write("<div style='background-color: black; color: white;'>черный</div>");
  90. } else {
  91. document.write("<div style='background-color: gray;'>Я не понял</div>");
  92. }
  93. alert('aborting execution to make changes. Reload page to return to select mode')
  94. loop = false
  95. break;
  96. case 'prompt: or':
  97. let age = prompt('Введите свой возраст', '') || alert('Вы не ввели ничего!')
  98. if (age && isNaN(age) || age > 200 || age < 0){
  99. alert('Вы ввели неверные данные!')
  100. } else if(age){
  101. age = age = Math.trunc(Number(age))
  102. res = new Date().getFullYear() - age
  103. alert(`Исходя из введенного вами возраста вы родились в ${res}г. либо ${res-1}г.`)
  104. }
  105. break;
  106. case 'confirm: or this days':
  107. confirm('шоппинг') || alert('ты-бяка')
  108. break;
  109. case 'confirm: if this days':
  110. if(!confirm('шоппинг')) {
  111. alert('ты-бяка')
  112. }
  113. break;
  114. case 'triple prompt':
  115. let Name = prompt('your Name:','')
  116. let surname = prompt('your Surname:','')
  117. let patronymic = prompt('your Patronymic:','')
  118. alert(`${surname} ${Name} ${patronymic}`)
  119. break;
  120. case 'default: or':
  121. let defaultName = prompt('your Name:','') || 'Иван'
  122. let defaultSurname = prompt('your Surname:','') || 'Иванов'
  123. let defaultPatronymic = prompt('your Patronymic:','') || 'Иванович'
  124. alert(`${defaultSurname} ${defaultName} ${defaultPatronymic}`)
  125. break;
  126. case 'default: if':
  127. let NameIf = prompt('your Name:','')
  128. let surnameIf = prompt('your Surname:','')
  129. let patronymicIf = prompt('your Patronymic:','')
  130. if(NameIf) {
  131. NameIf = NameIf
  132. } else {
  133. NameIf = 'Ivan'
  134. }
  135. if(surnameIf) {
  136. surnameIf = surnameIf
  137. } else {
  138. surnameIf = 'Ivanov'
  139. }
  140. if (patronymicIf) {
  141. patronymicIf = patronymicIf
  142. } else {
  143. patronymicIf = 'Ivanovich'
  144. }
  145. alert(`"if else" to add default values:
  146. ${surnameIf} ${NameIf} ${patronymicIf}`)
  147. break;
  148. case 'login and password':
  149. if(prompt('login','') === 'admin') {
  150. alert('correct login')
  151. if(prompt('password','') === 'qwerty') {
  152. alert('correct password')
  153. } else {
  154. alert('wrong password')
  155. }
  156. } else {
  157. alert('wrong login')
  158. }
  159. break;
  160. case 'currency calc':
  161. let curr = prompt('type currency: "eur" or "usd"','')
  162. let transferLog = {
  163. initial: 'hrn',
  164. to: '',
  165. ratios: {
  166. sold: {
  167. usd: 0.032,
  168. eur: 0.037,
  169. },
  170. bought: {
  171. usd: 0.033,
  172. eur: 0.038
  173. }
  174. }
  175. }
  176. if(curr) {
  177. // switch(curr.toLowerCase()) {
  178. // case 'eur':
  179. // transferLog.to = 'eur'
  180. // confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['usd'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['usd'], 'bought']
  181. // break;
  182. // case 'usd':
  183. // transferLog.to = 'usd'
  184. // confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['eur'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['eur'], 'bought']
  185. // break;
  186. // }
  187. if(curr.toLowerCase() === 'eur') {
  188. transferLog.to = 'eur'
  189. confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['usd'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['usd'], 'bought']
  190. }
  191. if(curr.toLowerCase() === 'usd') {
  192. transferLog.to = 'usd'
  193. confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['eur'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['eur'], 'bought']
  194. }
  195. if(transferLog.to === 'eur' || transferLog.to === 'usd') {
  196. transferLog.money = prompt('input amount of money to give', '')
  197. if(!+transferLog.money) {
  198. alert('invalid input')
  199. } else {
  200. transferLog.given = Number(transferLog.money)
  201. if(transferLog.curr[1] === 'sold') {
  202. transferLog.money = truncateFraction(transferLog.given / transferLog.curr[0])
  203. transferLog.message = `You've ${transferLog.curr[1]} ${transferLog.given}${transferLog.to} for ${transferLog.money}${transferLog.initial}`
  204. } else if(transferLog.curr[1] === 'bought') {
  205. transferLog.money = truncateFraction(transferLog.given * transferLog.curr[0])
  206. transferLog.message = `You've ${transferLog.curr[1]} ${transferLog.money}${transferLog.to} for ${transferLog.given}${transferLog.initial}`
  207. }
  208. transferLog.money > 0? alert(transferLog.message) : alert(`Looks like there is too little amount of hrn given to translate to ${transferLog.to}`)
  209. }
  210. } else {
  211. alert('invalid input')
  212. }
  213. } else {
  214. alert('No input.')
  215. }
  216. break;
  217. case 'scissors':
  218. let combos = {
  219. paper: {
  220. conter: 'scissors'
  221. },
  222. scissors: {
  223. conter: 'rock'
  224. },
  225. rock: {
  226. conter: 'paper'
  227. }
  228. }
  229. let user = prompt('rock, paper, scissors?').toLowerCase()
  230. if(user === 'rock' || user === 'paper' || user === 'scissors') {
  231. let computer = Math.random()
  232. if(computer >= 0.6) {
  233. computer = 'scissors'
  234. } else if(computer <= 0.3) {
  235. computer = 'paper'
  236. } else {
  237. computer = 'rock'
  238. }
  239. alert(`computer: ${computer}`)
  240. if(user === computer) {
  241. alert('TIE')
  242. } else {
  243. combos[computer].conter === user ? alert('USER WINS.') : alert('COMPUTER WINS.')
  244. }
  245. } else {
  246. alert('user disqualified! Compuer wins!')
  247. }
  248. break;
  249. case 'real data':
  250. let currReal = prompt('type currency: "eur" or "usd"','')
  251. if(currReal && currReal.toLowerCase() === 'eur' || currReal.toLowerCase() === 'usd') {
  252. currReal = currReal.toLowerCase()
  253. let sellReal = confirm('Sell press "Ok" | Buy press "Cancel"')
  254. let operationReal = sellReal? 'sold' : 'bought'
  255. let givenReal = prompt('input amount of money you give:','')
  256. if(givenReal && !isNaN(givenReal)) {
  257. givenReal = truncateFraction(Number(givenReal))
  258. fetch(`https://open.er-api.com/v6/latest/${currReal.toUpperCase()}`).then(res => res.json())
  259. .then(data => {
  260. let got = sellReal ? truncateFraction(givenReal * data.rates.UAH) : truncateFraction(givenReal / data.rates.UAH)
  261. if(got > 0) {
  262. sellReal ?
  263. alert(`You've ${operationReal} ${givenReal}${currReal} for ${got}hrn`):
  264. alert(`You've ${operationReal} ${got}${currReal} for ${givenReal}hrn`)
  265. } else {
  266. alert(`Looks like there is too little amount of money been given to make such operation`)
  267. }
  268. })
  269. } else {
  270. alert('invalid input')
  271. }
  272. } else {
  273. alert('invalid input')
  274. }
  275. break;
  276. case 'black belt':
  277. let combos2 = {
  278. paper: {
  279. conter: 'scissors'
  280. },
  281. scissors: {
  282. conter: 'rock'
  283. },
  284. rock: {
  285. conter: 'paper'
  286. }
  287. }
  288. let user2 = prompt('rock, paper, scissors?').toLowerCase()
  289. let computer2 = Math.random() >= 0.6 ? 'scissors' : (Math.random() <= 0.3 ? 'paper' : 'rock')
  290. alert(`computer: ${computer2}`)
  291. user2 === 'rock' || user2 === 'paper' || user2 === 'scissors'? (computer2 === user2? alert('TIE') : combos2[computer2].conter === user2 ? alert('USER WINS.') : alert('COMPUTER WINS.')) : alert('user disqualified! Compuer wins!')
  292. break;
  293. case null:
  294. loop = false
  295. break;
  296. default:
  297. alert('no matches found:(')
  298. }
  299. }