script2.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. setUsd: function(value) {
  170. this.usd = value
  171. },
  172. setEur: function(value) {
  173. this.eur = value
  174. }
  175. },
  176. bought: {
  177. usd: 0.033,
  178. eur: 0.038
  179. }
  180. }
  181. }
  182. if(curr) {
  183. // switch(curr.toLowerCase()) {
  184. // case 'eur':
  185. // transferLog.to = 'eur'
  186. // confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['usd'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['usd'], 'bought']
  187. // break;
  188. // case 'usd':
  189. // transferLog.to = 'usd'
  190. // confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['eur'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['eur'], 'bought']
  191. // break;
  192. // }
  193. if(curr.toLowerCase() === 'eur') {
  194. transferLog.to = 'eur'
  195. confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['usd'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['usd'], 'bought']
  196. }
  197. if(curr.toLowerCase() === 'usd') {
  198. transferLog.to = 'usd'
  199. confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['eur'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['eur'], 'bought']
  200. }
  201. if(transferLog.to === 'eur' || transferLog.to === 'usd') {
  202. transferLog.money = prompt('input amount of money to give', '')
  203. if(!+transferLog.money) {
  204. alert('invalid input')
  205. } else {
  206. transferLog.given = Number(transferLog.money)
  207. if(transferLog.curr[1] === 'sold') {
  208. transferLog.money = truncateFraction(transferLog.given / transferLog.curr[0])
  209. transferLog.message = `You've ${transferLog.curr[1]} ${transferLog.given}${transferLog.to} for ${transferLog.money}${transferLog.initial}`
  210. } else if(transferLog.curr[1] === 'bought') {
  211. transferLog.money = truncateFraction(transferLog.given * transferLog.curr[0])
  212. transferLog.message = `You've ${transferLog.curr[1]} ${transferLog.money}${transferLog.to} for ${transferLog.given}${transferLog.initial}`
  213. }
  214. transferLog.money > 0? alert(transferLog.message) : alert(`Looks like there is too little amount of hrn given to translate to ${transferLog.to}`)
  215. }
  216. } else {
  217. alert('invalid input')
  218. }
  219. } else {
  220. alert('No input.')
  221. }
  222. break;
  223. case 'scissors':
  224. let combos = {
  225. paper: {
  226. conter: 'scissors'
  227. },
  228. scissors: {
  229. conter: 'rock'
  230. },
  231. rock: {
  232. conter: 'paper'
  233. }
  234. }
  235. let user = prompt('rock, paper, scissors?').toLowerCase()
  236. if(user === 'rock' || user === 'paper' || user === 'scissors') {
  237. let computer = Math.random()
  238. if(computer >= 0.6) {
  239. computer = 'scissors'
  240. } else if(computer <= 0.3) {
  241. computer = 'paper'
  242. } else {
  243. computer = 'rock'
  244. }
  245. alert(`computer: ${computer}`)
  246. if(user === computer) {
  247. alert('TIE')
  248. } else {
  249. combos[computer].conter === user ? alert('USER WINS.') : alert('COMPUTER WINS.')
  250. }
  251. } else {
  252. alert('user disqualified! Compuer wins!')
  253. }
  254. break;
  255. case 'real data':
  256. let currReal = prompt('type currency: "eur" or "usd"','')
  257. if(currReal && currReal.toLowerCase() === 'eur' || currReal.toLowerCase() === 'usd') {
  258. currReal = currReal.toLowerCase()
  259. let sellReal = confirm('Sell press "Ok" | Buy press "Cancel"')
  260. let operationReal = sellReal? 'sold' : 'bought'
  261. let givenReal = prompt('input amount of money you give:','')
  262. if(givenReal && !isNaN(givenReal)) {
  263. givenReal = truncateFraction(Number(givenReal))
  264. fetch(`https://open.er-api.com/v6/latest/${currReal.toUpperCase()}`).then(res => res.json())
  265. .then(data => {
  266. let got = sellReal ? truncateFraction(givenReal * data.rates.UAH) : truncateFraction(givenReal / data.rates.UAH)
  267. if(got > 0) {
  268. sellReal ?
  269. alert(`You've ${operationReal} ${givenReal}${currReal} for ${got}hrn`):
  270. alert(`You've ${operationReal} ${got}${currReal} for ${givenReal}hrn`)
  271. } else {
  272. alert(`Looks like there is too little amount of money been given to make such operation`)
  273. }
  274. })
  275. } else {
  276. alert('invalid input')
  277. }
  278. } else {
  279. alert('invalid input')
  280. }
  281. break;
  282. case 'black belt':
  283. let combos2 = {
  284. paper: {
  285. conter: 'scissors'
  286. },
  287. scissors: {
  288. conter: 'rock'
  289. },
  290. rock: {
  291. conter: 'paper'
  292. }
  293. }
  294. let user2 = prompt('rock, paper, scissors?').toLowerCase()
  295. let computer2 = Math.random() >= 0.6 ? 'scissors' : (Math.random() <= 0.3 ? 'paper' : 'rock')
  296. alert(`computer: ${computer2}`)
  297. 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!')
  298. break;
  299. case null:
  300. loop = false
  301. break;
  302. default:
  303. alert('no matches found:(')
  304. }
  305. }