функции.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //Number: temperature
  2. let fahrenheit = x => x * 1.8 + 32 // функция конвертации из цельсия в фарингейты
  3. console.log(fahrenheit(20))//68 градусов по фарингейту
  4. //Flats
  5. let flats = (apartmentNumber,floorNumber,numberApartmentsFloor)=>{
  6. let sumApartments = floorNumber*numberApartmentsFloor// здесь мы узнаем сколько квартир в подьезде
  7. let entrance = Math.ceil(apartmentNumber/sumApartments)//номер подьезда
  8. let floor = Math.ceil((apartmentNumber%sumApartments)/numberApartmentsFloor)//номер этажа
  9. return {entrance,floor}
  10. }
  11. console.log(flats(65,9,4))//{entrance: 2, floor: 8}
  12. //Credentials
  13. {
  14. let credentials = ()=>{
  15. let name = prompt("Введите имя").trim().toLowerCase();
  16. let surname = prompt("Введите фамилию").trim().toLowerCase();
  17. let fatherName = prompt("Введите отчество").trim().toLowerCase();
  18. name = name[0].toUpperCase()+name.slice(1);
  19. surname = surname[0].toUpperCase()+surname.slice(1);
  20. fatherName = fatherName[0].toUpperCase()+fatherName.slice(1);
  21. fullName = name+' '+surname+' '+fatherName
  22. return {name,surname,fatherName,fullName}
  23. }
  24. console.log(credentials())
  25. }
  26. //New line
  27. {
  28. let newLine = (str)=>{
  29. let result = (str.split('\\n').join('\n'))
  30. return result
  31. }
  32. console.log(newLine('1 строка \n 2 строка'))
  33. }
  34. //Prompt OR
  35. {
  36. let promptOr = (age) => age ? alert('Вы ' +(2022 - age)+ ' года рождения!') :alert('Ошибка')
  37. console.log(promptOr(+prompt('Сколько вам лет?')))
  38. }
  39. //Login And Password
  40. {
  41. let loginAndPassword = (login, pass) => login === 'admin' && pass === 'qwerty' ? console.log(true) : console.log(false)
  42. console.log(loginAndPassword('admin','qwerty'))//true
  43. }
  44. //For Table
  45. {
  46. 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]]
  47. let table = (array) => {
  48. let str = "<table border='1'>"
  49. for (let currency of arr){
  50. str += '<tr>'
  51. for (let letter of currency){
  52. str += '<td>'+letter+'</td>'
  53. }
  54. str+= '</tr>'
  55. }
  56. str+= "</table>"
  57. return str
  58. }
  59. document.write(table(arr))
  60. }
  61. //Filter Lexics
  62. {
  63. let func = (str,motherWordArr)=>{
  64. str = str.split(' ')
  65. let result = str.filter(element => motherWordArr.indexOf(element)=== -1)
  66. result = result.join(' ')
  67. return result
  68. }
  69. console.log(func('Функция должна возвращать бляха муха строку!',['бляха', 'муха', "пляха", "шабля"]))
  70. }
  71. //Currency Table
  72. {
  73. let func = ()=>{
  74. fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
  75. .then(data => {
  76. let dataObj = data.rates
  77. let arrValues = Object.values(dataObj)
  78. let arrKeys = Object.keys(dataObj)
  79. let arrData = []
  80. let i = 0
  81. for(let valuesEl of arrValues){
  82. arrData.push(arrValues.map(x=>(valuesEl/x).toFixed(3)))
  83. arrData[i].unshift(arrKeys[i])
  84. i++
  85. }
  86. arrKeys.unshift('')
  87. arrData.unshift(arrKeys)
  88. let currencyTable = "<table border='1'>"
  89. for (let arrEl of arrData){
  90. currencyTable += '<tr>'
  91. for (let cell of arrEl){
  92. currencyTable += '<td>'+cell+'</td>'
  93. }
  94. currencyTable+= '</tr>'
  95. }
  96. currencyTable+= "</table>"
  97. document.write(currencyTable)
  98. })
  99. }
  100. console.log(func())
  101. }
  102. //Form
  103. {
  104. let func = (obj) => {
  105. let keysArrCar = Object.keys(obj)
  106. let valuesArrCar = Object.values(obj)
  107. let i = 0
  108. let strType
  109. let formObjCar = '<form>\n'
  110. for (let a of keysArrCar){
  111. if (typeof valuesArrCar[i]=='string'){
  112. strType = 'text'
  113. }
  114. else if(typeof valuesArrCar[i]=='boolean'){
  115. strType = 'checkbox'
  116. }
  117. else{strType=typeof valuesArrCar[i]}
  118. formObjCar += ' <label>'+a+': <input type="'+strType+'" value="'+valuesArrCar[i]+'"/></label>\n'
  119. i++
  120. }
  121. formObjCar += '</form>'
  122. document.write(formObjCar)
  123. }
  124. const car = {
  125. "Name":"chevrolet chevelle malibu",
  126. "Cylinders":8,
  127. "Displacement":307,
  128. "Horsepower":130,
  129. "Weight_in_lbs":3504,
  130. "Origin":"USA",
  131. "in_production": false
  132. }
  133. console.log(func(car))
  134. }
  135. //Array of objects sort
  136. {
  137. let sort = (arrObj,key,revers)=>{
  138. return arrObj.sort((firstEl, secondEl) =>{
  139. if (typeof firstEl[key] === 'string' ){
  140. return revers===undefined || revers===true?firstEl[key].localeCompare(secondEl[key]):secondEl[key].localeCompare(firstEl[key])
  141. }
  142. else if (typeof firstEl[key] === 'number'){
  143. return revers===undefined || revers===true?firstEl[key] - secondEl[key]:secondEl[key]-firstEl[key]
  144. }
  145. })
  146. }
  147. var persons = [
  148. {name: "Иван", age: 17},
  149. {name: "Мария", age: 35},
  150. {name: "Алексей", age: 73},
  151. {name: "Яков", age: 12},
  152. ]
  153. //console.log(sort(persons, "age"))
  154. console.log(sort(persons, "name",false))
  155. }
  156. //Table
  157. {
  158. let func = (arrayObj,keyObj,revers)=>{
  159. let sort = (arrObj,key,revers)=>{
  160. return arrObj.sort((firstEl, secondEl) =>{
  161. if (typeof firstEl[key] === 'string' ){
  162. return revers===undefined || revers===true?firstEl[key].localeCompare(secondEl[key]):secondEl[key].localeCompare(firstEl[key])
  163. }
  164. else if (typeof firstEl[key] === 'number'){
  165. return revers===undefined || revers===true?firstEl[key] - secondEl[key]:secondEl[key]-firstEl[key]
  166. }
  167. })
  168. }
  169. let [...arrCopi] = arrayObj
  170. persons= sort(arrCopi,keyObj,revers)
  171. let i = 0
  172. let arrKeys = []
  173. for (let x of persons){
  174. arrKeys += Object.keys(persons[i])+','
  175. i++
  176. }
  177. arrKeys = arrKeys.split(',').slice(0,-1)
  178. const makeUniq = (arr) => {
  179. return arr.filter((el1, el2) => arr.indexOf(el1) === el2);
  180. }
  181. arrKeys = makeUniq(arrKeys)
  182. let arrValue = []
  183. for (let objPersons of persons){
  184. arrValue.push(arrKeys.map(el=>objPersons[el]===undefined?objPersons[el]=' ':objPersons[el]))
  185. }
  186. arrValue.unshift(arrKeys)
  187. let arrTable = "<table border='1'>"
  188. for (let arrEl of arrValue){
  189. arrTable += '<tr>'
  190. for (let cell of arrEl){
  191. arrTable += '<td>'+cell+'</td>'
  192. }
  193. arrTable+= '</tr>'
  194. }
  195. arrTable+= "</table>"
  196. document.write(arrTable)
  197. }
  198. const pers = [
  199. {
  200. name: 'Мария',
  201. fatherName: 'Ивановна',
  202. surname: 'Иванова',
  203. sex: 'female'
  204. },
  205. {
  206. name: 'Николай',
  207. fatherName: 'Иванович',
  208. surname: 'Иванов',
  209. age: 15
  210. },
  211. {
  212. name: 'Петр',
  213. fatherName: 'Иванович',
  214. surname: 'Иванов',
  215. married: true
  216. },
  217. ]
  218. console.log(func(pers,'surname'))
  219. }