script1.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //assign: evaluation
  2. var a = 5;
  3. var b, c;
  4. c = (b = (a * 5) / 2);
  5. // b = (a * 5);
  6. // b = (c = b/2);
  7. // a = 5
  8. // b = 12,5
  9. // c = 12,5
  10. // forgot one semicolon!
  11. // for(let i = 0 i < 5; i++) {
  12. // console.log(i)
  13. // }
  14. // wrong usage of semicolon
  15. //let a; b = 5;
  16. //b = 5 c = b + 1;
  17. //semicolon logic errors
  18. // if(a === 5) {
  19. // console.log('FIVE!')
  20. // }else; console.log('not five')
  21. //Number: age
  22. function getAge(age, currentYear = new Date().getFullYear()) {
  23. try {
  24. age = Math.trunc(Number(age))
  25. if(!age || isNaN(age) || age > 200 || age < 0) {
  26. throw e
  27. } else {
  28. currentYear = currentYear - age
  29. return [currentYear, currentYear-1]
  30. }
  31. } catch(e) {
  32. return 0
  33. }
  34. }
  35. let year = getAge(prompt("input your age",""))
  36. if(year) {
  37. alert(`Looks like you've been born in ${year[0]} or ${year[1]}`)
  38. } else {
  39. alert('whoops, looks like invalid input value has been given!')
  40. }
  41. //Number: temperature
  42. try {
  43. let grad = Number(prompt("Input temperature",""))
  44. let sys = prompt(`
  45. Type:
  46. F - to translate to Fahrenheit
  47. C - to translate to Celsius
  48. `)
  49. let res = getTemperature(grad, sys)
  50. if(res) {
  51. alert(`${grad}${sys.toLowerCase() === 'c'? '°F':'°C'} = ${res}${sys.toLowerCase() === 'c'? '°C':'°F'}`)
  52. } else {
  53. throw e;
  54. }
  55. } catch(e) {
  56. alert('invalid input!')
  57. }
  58. function getTemperature(value, system="c") {
  59. if(isNaN(Number(value))) return false
  60. switch(system.toLowerCase()) {
  61. case 'f':
  62. return ((value * 9/5) + 32).toFixed(2)
  63. case 'c':
  64. return ((value - 32) * 5/9).toFixed(2)
  65. default:
  66. return false
  67. }
  68. }
  69. //Number: divide
  70. function flooredDivide(num1, num2) {
  71. console.log(num1 / num2)
  72. return Math.floor(num1 / num2)
  73. }
  74. //Number: odd
  75. function oddInformer() {
  76. let num = prompt("input a number:", "")
  77. if(isNaN(+num) || !num) {
  78. alert("not a number!!!")
  79. } else {
  80. if(num % 2 !== 0) {
  81. alert(`${num} is odd`)
  82. } else {
  83. alert(`${num} is even`)
  84. }
  85. }
  86. }
  87. //String: greeting
  88. //greet user using alert() and prompt()
  89. function greeting() {
  90. let name = prompt("Hey user, what's your name")
  91. alert(`Hello, ${name? name : 'Namless One...'}`)
  92. }
  93. //String: lexis
  94. function lexis(string, word) {
  95. if(string.indexOf(word) > 0) {
  96. return true
  97. } else {
  98. return false
  99. }
  100. }
  101. //confirm возвращает true либо false по нажатию на кнопки 'Ок' или 'Отмена' соответственно.
  102. //Boolean
  103. let quest1 = confirm('are you a human being?')
  104. let quest2 = confirm('can you fly?')
  105. if(quest1) {
  106. if(quest2) {
  107. alert('wow, you are Superman!')
  108. } else {
  109. alert('Congrats, you are a real human being...')
  110. }
  111. } else {
  112. if(quest2) {
  113. alert('so u must be a bird or smth :/')
  114. } else {
  115. alert('is machine spirit talking to me?')
  116. }
  117. }
  118. //Array: real
  119. let inMyBag = ['shoes', 'shorts', 'shirt', 'laptop', 'charger', 'copybook', 'pen', 'water-bottle']
  120. //Array: booleans
  121. let answers = [quest1, quest2]
  122. //Array: plus
  123. let plus = [1, 2]
  124. plus[2] = plus[0] + plus[1]
  125. console.log(plus)
  126. //Array: plus string
  127. //возможность можно использовать что бы создавать новые строки из множества строчных значений
  128. let plusStr = ['hello', 'My', 'World',]
  129. plusStr[plusStr.length] = plusStr[0] + plusStr[1] + plusStr[2]
  130. console.log(plusStr)
  131. // Object: real
  132. let keyboard = {
  133. model: 'sven slim 303',
  134. type: 'membrane',
  135. price: '5$'
  136. }
  137. let guitar = {
  138. type: 'electric',
  139. form: 'stratocaster',
  140. strings: 6,
  141. manufacturer: 'Fender'
  142. }
  143. //Object: change
  144. keyboard.price = '10$'
  145. guitar['form'] = 'telecaster'
  146. // Comparison if
  147. var age = +prompt("Сколько вам лет?","");
  148. if (age < 18 && age > 0){
  149. alert("школьник");
  150. } else {
  151. if (age >= 18 && age < 30){
  152. alert("молодежь");
  153. } else {
  154. if (age >= 30 && age < 45){
  155. alert("зрелость");
  156. } else {
  157. if (age >= 45 && age < 60){
  158. alert("закат");
  159. } else {
  160. if (age >= 60 && age <= 150 ) {
  161. alert("как пенсия?");
  162. } else {
  163. alert("то ли киборг, то ли ошибка");
  164. }
  165. }
  166. }
  167. }
  168. }
  169. //Comparison: sizes
  170. let rusSize = prompt('Введите НАШ формат размера для переводa на на Американский формат','')
  171. let usSize = [
  172. {
  173. number: [6],
  174. men: 'S',
  175. woman:['XXS']
  176. },
  177. {
  178. number: [8, 10],
  179. men: 'M',
  180. woman:['XXS', 'XS']
  181. },
  182. {
  183. number: [12, 14],
  184. men: 'L',
  185. woman:['S', 'M']
  186. },
  187. {
  188. number: [16, 18],
  189. men: 'XL',
  190. woman:['L','XL']
  191. },
  192. {
  193. number: [20, 22],
  194. men: 'XXL',
  195. woman:['XXL', 'XXXL']
  196. },
  197. ]
  198. if(+rusSize && +rusSize >= 40 && +rusSize <= 56) {
  199. rusSize = +rusSize
  200. let flag = 0
  201. if(rusSize % 2 !== 0) {
  202. rusSize--
  203. flag++
  204. }
  205. for(let i = 0; i < usSize.length; i++) {
  206. let temp = usSize[i].number.findIndex(element => { return element === (rusSize - 34)})
  207. if(temp >= 0) {
  208. alert(`
  209. ${rusSize+flag}-й размер в американском формате:
  210. ------------------------------------------
  211. Размер: ${usSize[i].number[temp]}
  212. Мужчины: ${usSize[i].men}
  213. Женщины: ${usSize[i].woman[temp]}
  214. ------------------------------------------
  215. `)
  216. break;
  217. }
  218. }
  219. } else {
  220. alert('Неправильный размер.')
  221. }
  222. //Ternary
  223. alert(`${confirm('Вы мужчина?')? 'Вы мужчина.':'Вы женщина.'}`)
  224. //flats
  225. function flats(floors, flatsPerFloor, flatNumber) {
  226. let info = {
  227. flat: flatNumber,
  228. building: Math.ceil(flatNumber/(floors * flatsPerFloor)),
  229. }
  230. info.floor = flatNumber/flatsPerFloor > floors? Math.ceil(flatNumber/flatsPerFloor)-floors*(info.building-1) : Math.ceil(flatNumber/flatsPerFloor);
  231. return info;
  232. }