main.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //Confirms
  2. {
  3. let arr = [confirm("Are you man?"), confirm("Are you woman?"), confirm("Are you human?")]
  4. alert(arr)
  5. }
  6. //Prompts
  7. {
  8. let arr = [prompt("Are you man?"), prompt("Are you woman?"), prompt("Are you human?") ]
  9. alert(arr[1])
  10. }
  11. //Item access
  12. {
  13. let arr = ["cat", "dog", "elephant"]
  14. let userAnswer = +prompt("Choose number of element (0-2)")
  15. alert( arr[userAnswer] + ", length=" + arr.length)
  16. }
  17. //Item access
  18. {
  19. let arr=[1,2,3,4,5,6,7,8,9]
  20. let userAnswer = +prompt("Choose number of element (0-9)")
  21. console.log(arr[userAnswer])
  22. let userAnswer2 = +prompt("Write value for your choise")
  23. arr[userAnswer] = userAnswer2
  24. alert(arr[userAnswer] + ", all array: " + arr)
  25. }
  26. //Multiply table
  27. {
  28. let arr = [[0,0,0,0,0], [0,1,2,3,4], [0,2,4,6,8], [0,3,6,9,12], [0,4,8,12,16]]
  29. }
  30. //Multiply table slice
  31. {
  32. let arr = [[0,0,0,0,0], [0,1,2,3,4], [0,2,4,6,8], [0,3,6,9,12], [0,4,8,12,16]]
  33. arr[1] = arr[1].slice(-4)
  34. arr[2] = arr[2].slice(-4)
  35. arr[3] = arr[3].slice(-4)
  36. arr[4] = arr[4].slice(-4)
  37. let firstArr = arr.slice(-4)
  38. alert(firstArr)
  39. }
  40. //IndexOf Word
  41. {
  42. let userText = prompt("Write some text")
  43. let userWord = prompt("Write word for search")
  44. let result = userText.indexOf(userWord)
  45. if(result >=0){
  46. alert("Position of your word " + result)
  47. }
  48. else{
  49. alert('Error')
  50. }
  51. }
  52. //Reverse
  53. {
  54. const userArr = []
  55. userArr.push(prompt("Wtite your 1 element"))
  56. userArr.push(prompt("Wtite your 2 element"))
  57. userArr.push(prompt("Wtite your 3 element"))
  58. userArr.push(prompt("Wtite your 4 element"))
  59. userArr.push(prompt("Wtite your 5 element"))
  60. let newArr = []
  61. newArr.push(userArr.pop())
  62. newArr.push(userArr.pop())
  63. newArr.push(userArr.pop())
  64. newArr.push(userArr.pop())
  65. newArr.push(userArr.pop())
  66. alert(newArr)
  67. }
  68. //Reverse 2
  69. {
  70. let secondArr = []
  71. secondArr.unshift(newArr.shift())
  72. secondArr.unshift(newArr.shift())
  73. secondArr.unshift(newArr.shift())
  74. secondArr.unshift(newArr.shift())
  75. secondArr.unshift(newArr.shift())
  76. alert(secondArr)
  77. }
  78. //Copy
  79. {
  80. let arr = [[0,0,0,0,0], [0,1,2,3,4], [0,2,4,6,8], [0,3,6,9,12], [0,4,8,12,16]]
  81. let copy = [...arr]
  82. }
  83. //Deep copy
  84. {
  85. let arr = [[0,0,0,0,0], [0,1,2,3,4], [0,2,4,6,8], [0,3,6,9,12], [0,4,8,12,16]]
  86. let copy = arr.slice()
  87. arr.push(999)
  88. alert(copy)
  89. }
  90. //Array Equals
  91. {
  92. let x,y
  93. const arr = [x,y]
  94. const arr1 = arr
  95. const arr2 = arr
  96. arr1 === arr2
  97. }
  98. //Flat
  99. {
  100. let arr = [[0,0,0,0,0], [0,1,2,3,4], [0,2,4,6,8], [0,3,6,9,12], [0,4,8,12,16]]
  101. const arrSpread = [...arr[0], ...arr[1],...arr[2],...arr[3],...arr[4]].length
  102. alert("Length " + arrSpread)
  103. }
  104. //Destruct
  105. {
  106. let userText = prompt("Write long text without space").split("")
  107. const [a,,,,b,,,,c,others] = userText
  108. alert(a + b + c)
  109. }
  110. //Destruct default
  111. {
  112. let userText = prompt("Write long text without space").split("")
  113. const [a=10,,,,b=20,,,,c=99,others] = userText
  114. alert(a + b + c)
  115. }
  116. //Multiply table rest
  117. {
  118. let arr = [[0,0,0,0,0], [0,1,2,3,4], [0,2,4,6,8], [0,3,6,9,12], [0,4,8,12,16]]
  119. const [a, ...arr1] = arr
  120. const [b, ...arr2] = arr1[0]
  121. const [c, ...arr3] = arr1[1]
  122. const [d, ...arr4] = arr1[2]
  123. const [e, ...arr5] = arr1[3]
  124. const arrFinal = []
  125. arrFinal.push(arr2, arr3, arr4, arr5)
  126. alert(arrFinal)
  127. }
  128. //For Alert
  129. {
  130. const arr = ["John", "Paul", "George", "Ringo"]
  131. for (let name of arr){
  132. alert(`Hi,${name}`)
  133. }
  134. }
  135. //For Select Option
  136. {
  137. const currencies = ["USD", "EUR", "GBP", "UAH"]
  138. let str = "<select>"
  139. for (let currency of currencies){
  140. str += "<option>" + currency + "</option>"
  141. }
  142. str+= "</select>"
  143. document.write(str) //document.write отобразит ваш HTML на странице
  144. }
  145. //For Table Horizontal
  146. {
  147. const names = ["John", "Paul", "George", "Ringo"]
  148. let str = "<table>"
  149. for (let name of names){
  150. str += "<tr>" + name + "</tr"
  151. }
  152. str+= "</table>"
  153. document.write(str)
  154. }
  155. //For Table Vertical
  156. {
  157. const names = ["John", "Paul", "George", "Ringo"]
  158. let str = "<table>"
  159. for (let name of names){
  160. str += "<tr>"
  161. str += "<td>" + name + "</td>"
  162. str += "</tr>"
  163. }
  164. str+= "</table>"
  165. document.write(str)
  166. }
  167. //For Table Letters
  168. {
  169. const currencies = ["USD", "EUR", "GBP", "UAH"]
  170. let str = "<table>"
  171. for (let currency of currencies){ //цикл создает строки
  172. //одна итерация цикла создает ОДНУ СТРОКУ
  173. str += "<tr>"
  174. str += "</tr>"
  175. console.log(currency)
  176. for (let letter of currency){ //цикл создает ЯЧЕЙКИ в ОДНОЙ СТРОКЕ
  177. //одна итерация цикла создает ОДНУ ЯЧЕЙКУ
  178. str += "<td>" + letter + "</td>"
  179. console.log(letter)
  180. }
  181. }
  182. str+= "</table>"
  183. document.write(str)
  184. }
  185. //For Multiply Table
  186. {
  187. let arr = [[0,0,0,0,0], [0,1,2,3,4], [0,2,4,6,8], [0,3,6,9,12], [0,4,8,12,16]]
  188. let str = "<table>"
  189. let i = 0
  190. for (let number of arr){
  191. str += "<tr>" + "<tr/>"
  192. for (let letter of number){
  193. str += (i % 2) ? "<td style=background-color:#FF0000>" + letter + "<td>" : "<td style=background-color:#000FFF>" + letter + "<td>"
  194. }
  195. i++
  196. }
  197. document.write(str)
  198. }
  199. // Function Capitalize
  200. {
  201. const capitalize = str => {
  202. let result = str.split('')[0].toUpperCase() + str.slice(1,8).toLowerCase()
  203. return result //именно этот код обеспечит возврат результата функции
  204. }
  205. console.log(capitalize("cANBerRa")) //Canberra
  206. }
  207. //Map Capitalize
  208. {
  209. const answer = prompt("Write something").split(" ")
  210. const firstBig = answer.map(x => x[0].toUpperCase() + x.slice(1).toLowerCase())
  211. let result = firstBig.join(" ")
  212. alert(result)
  213. }
  214. //Filter Lexics
  215. {
  216. const answer = prompt("Напиши шось").split(" ")
  217. const badWords = ["бляха", "пиздос", "лох"]
  218. let result = answer.filter(x => badWords.includes(x)).join(" ")? false : true
  219. console.log(result)
  220. alert(result)
  221. }
  222. //Beep Lexics
  223. {
  224. let answer = prompt("Напиши шось").split(" ")
  225. let badWords = ["бляха", "пиздос", "лох"]
  226. let result = answer.map(x => badWords.includes(x)? ("BEEP") : x )
  227. console.log(result.join(" "))
  228. alert(result.join(" "))
  229. }
  230. //Reduce HTML
  231. {
  232. const currencies = ["USD", "EUR", "GBP", "UAH"]
  233. let str = "<select>"
  234. str += currencies.reduce( (a,b) => a + "<option>" + b + "</option>","")
  235. str += "</select>"
  236. document.write(str)
  237. }