Массивы.js 6.6 KB

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