script1.js 6.0 KB

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