Массивы.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //Confirms
  2. {
  3. const arr = [confirm('Ты мужчина?'),confirm('Ты женщина?'),confirm('Тебе больше 18-ти лет?')]
  4. console.log(arr)//[true, false, true]
  5. }
  6. //Prompts
  7. {
  8. const arr = []
  9. arr[0]=prompt('Ты мужчина?')
  10. arr[1]=prompt('Ты женщина?')
  11. arr[2]=prompt('Тебе больше 18-ти лет?')
  12. console.log(arr)// ['да', 'нет', 'да']
  13. }
  14. //Item access
  15. {
  16. const arr = ['нулевой','первый','второй']
  17. indexArr = prompt('Введите индекс массива')
  18. console.log(arr[indexArr])
  19. }
  20. //Item change
  21. {
  22. const arr = ['нулевой','первый','второй']
  23. indexArr = prompt('Введите индекс массива')//если тут будет '2'
  24. meaningArr = prompt('Введите значение для выбраного индекса')// а тут слово 'четвертый'
  25. arr[indexArr]= meaningArr
  26. console.log(arr)// то здесь будет такой массив ['нулевой', 'первый', 'четвертый']
  27. }
  28. //Multiply table
  29. {
  30. const 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]]
  31. console.log(arr[3][4])// ответ 12
  32. }
  33. //Multiply table slice
  34. {
  35. const 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]]
  36. let arr2 = [arr[1].slice(1),arr[2].slice(1),arr[3].slice(1),arr[4].slice(1)]
  37. }
  38. //IndexOf Word
  39. {
  40. let str = prompt('Введите строку из несколький слов').split(' ');
  41. let strIndex = prompt('Введите искомое слово');
  42. let arr = str.indexOf(strIndex);
  43. if (arr === -1){
  44. alert('Слово не найдено');
  45. }
  46. else{alert('Ваше слово '+(arr+1)+'-е по счету');}
  47. }
  48. //Reverse
  49. {
  50. let arr = []
  51. arr.push(prompt('Введите первый элемент'))
  52. arr.push(prompt('Введите второй элемент'))
  53. arr.push(prompt('Введите третий элемент'))
  54. arr.push(prompt('Введите четвертый элемент'))
  55. arr.push(prompt('Введите пятый элемент'))
  56. let arr2 = []
  57. arr2.push(arr.pop())
  58. arr2.push(arr.pop())
  59. arr2.push(arr.pop())
  60. arr2.push(arr.pop())
  61. arr2.push(arr.pop())
  62. console.log(arr2)
  63. //Reverse 2
  64. let arr3 = []
  65. arr3.unshift(arr2.shift())
  66. arr3.unshift(arr2.shift())
  67. arr3.unshift(arr2.shift())
  68. arr3.unshift(arr2.shift())
  69. arr3.unshift(arr2.shift())
  70. }
  71. //Copy
  72. {
  73. const 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]]
  74. const arr2 = arr
  75. }
  76. //Deep Copy
  77. {
  78. const 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]]
  79. const arr2 = arr.slice()
  80. }
  81. //Array Equals
  82. {
  83. const arr = [1,2,3]
  84. const arr2 = arr
  85. arr === arr2//true
  86. }
  87. //Flat
  88. {
  89. const 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]]
  90. const arr2 = [...arr[0],...arr[1],...arr[2],...arr[3],...arr[4]]
  91. console.log(arr2.length)//25
  92. }
  93. //Destruct
  94. {
  95. let str = prompt('Введите текст')
  96. const [a,,,,b,,,,c] = str
  97. alert(a+b+c)
  98. }
  99. //Destruct default
  100. {
  101. let str = prompt('Введите текст')
  102. const [a='!',,,,b='!',,,,c='!'] = str
  103. alert(a+b+c)
  104. }
  105. //Multiply table rest
  106. {
  107. const 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]]
  108. const [,[,...a],[,...b],[,...c],[,...d]] = arr
  109. const arr2 = [a,b,c,d]
  110. const arr3 = [...arr2[0],...arr2[1],...arr2[2],...arr2[3]]
  111. console.log(arr3) // [1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16]
  112. }
  113. //For Alert
  114. {
  115. const names = ["John", "Paul", "George", "Ringo"]
  116. for (let name of names)
  117. {
  118. alert(`Hello, ${name}`)
  119. }
  120. }
  121. //For Select Option
  122. {
  123. const currencies = ["USD", "EUR", "GBP", "UAH"]
  124. let str = "<select>"
  125. for (let currency of currencies){
  126. str += '<option>'+currency+'</option>'
  127. }
  128. str+= "</select>"
  129. document.write(str)
  130. }
  131. //For Table Horizontal
  132. {
  133. const names = ["John", "Paul", "George", "Ringo"]
  134. let str = "<table border='1'>"
  135. for (let namesTable of names){
  136. str += '<td>'+namesTable+'</td>'
  137. }
  138. str+= "</table>"
  139. document.write(str)
  140. }
  141. //For Table Vertical
  142. {
  143. const names = ["John", "Paul", "George", "Ringo"]
  144. let str = "<table border='1'>"
  145. for (let namesTable of names){
  146. str += '<tr><td>'+namesTable+'</td></tr>'
  147. }
  148. str+= "</table>"
  149. document.write(str)
  150. }
  151. //For Table Letters
  152. {
  153. const currencies = ["USD", "EUR", "GBP", "UAH"]
  154. let str = "<table border='1'>"
  155. for (let currency of currencies){
  156. str += '<tr>'
  157. console.log(currency)
  158. for (let letter of currency){
  159. str += '<td>'+letter+'</td>'
  160. console.log(letter)
  161. }
  162. str += '</tr>'
  163. }
  164. str+= "</table>"
  165. document.write(str)
  166. }
  167. //For Multiply Table
  168. {
  169. const 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]]
  170. let str = "<table border='1'>"
  171. for (let currency of arr){
  172. str += '<tr>'
  173. for (let letter of currency){
  174. str += '<td>'+letter+'</td>'
  175. }
  176. str+= '</tr>'
  177. }
  178. str+= "</table>"
  179. document.write(str)
  180. }
  181. //Function Capitalize
  182. {
  183. const capitalize = str => {
  184. let result
  185. result=str[0].toUpperCase()+str.toLowerCase().slice(1)
  186. return result
  187. }
  188. console.log(capitalize("cANBerRa")) //Canberra
  189. }
  190. //Map Capitalize
  191. {
  192. let str = prompt('Введите строку').split(' ')
  193. const capitalize = str => {
  194. let result = str[0].toUpperCase()+str.toLowerCase().slice(1)
  195. return result
  196. }
  197. let arr = str.map(capitalize).join(' ')
  198. alert(arr)
  199. }
  200. //Filter Lexics
  201. const arr = ['бляха', 'муха', "пляха", "шабля"]
  202. let motherWord = prompt('Введите строку').split(' ')
  203. let result = motherWord.filter(element => arr.indexOf(element)=== -1)
  204. alert(result.join(' '))
  205. //Beep Lexics
  206. {
  207. const arr = ['бляха', 'муха', "пляха", "шабля"]
  208. let motherWord = prompt('Введите строку').split(' ')
  209. let arr2 = []
  210. for (let el of arr){
  211. motherWord = motherWord.map(x => x===el ?x='BEEP':x)
  212. }
  213. alert(motherWord.join(' '))
  214. }
  215. //Reduce HTML
  216. {
  217. const currencies = ["","USD", "EUR", "GBP", "UAH"]
  218. let str = "<select>"
  219. str += currencies.reduce( (a,b) => a+'<option>'+b+'</option>' )
  220. str += "</select>"
  221. document.write(str)
  222. }