Функции 2.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //Arrow to Functions
  2. //Переведите любые пять заданий из предыдущего ДЗ по функциям в синтаксис function
  3. //Number: temperature
  4. {
  5. function fahrenheit(x){return x * 1.8 + 32} // функция конвертации из цельсия в фарингейты
  6. console.log(fahrenheit(20))//68 градусов по фарингейту
  7. }
  8. //Flats
  9. {
  10. function flats (apartmentNumber,floorNumber,numberApartmentsFloor){
  11. let sumApartments = floorNumber*numberApartmentsFloor// здесь мы узнаем сколько квартир в подьезде
  12. let entrance = Math.ceil(apartmentNumber/sumApartments)//номер подьезда
  13. let floor = Math.ceil((apartmentNumber%sumApartments)/numberApartmentsFloor)//номер этажа
  14. return {entrance,floor}
  15. }
  16. console.log(flats(65,9,4))//{entrance: 2, floor: 8}
  17. }
  18. //Credentials
  19. {
  20. function credentials (){
  21. let name = prompt("Введите имя").trim().toLowerCase();
  22. let surname = prompt("Введите фамилию").trim().toLowerCase();
  23. let fatherName = prompt("Введите отчество").trim().toLowerCase();
  24. name = name[0].toUpperCase()+name.slice(1);
  25. surname = surname[0].toUpperCase()+surname.slice(1);
  26. fatherName = fatherName[0].toUpperCase()+fatherName.slice(1);
  27. fullName = name+' '+surname+' '+fatherName
  28. return {name,surname,fatherName,fullName}
  29. }
  30. console.log(credentials())
  31. }
  32. //New line
  33. {
  34. function newLine (str){
  35. let result = (str.split('\\n').join('\n'))
  36. return result
  37. }
  38. console.log(newLine('1 строка\n2 строка'))
  39. }
  40. //Prompt OR
  41. {
  42. function promptOr(age){
  43. age ? alert('Вы ' +(2022 - age)+ ' года рождения!') :alert('Ошибка')
  44. }
  45. console.log(promptOr(+prompt('Сколько вам лет?')))
  46. }
  47. //createPerson
  48. {
  49. function createPerson(name, surname){
  50. let obj = {
  51. 'name': name,
  52. 'surname': surname,
  53. getFullName: function (){
  54. if(obj.fatherName){
  55. return `${this.name} ${this.fatherName} ${this.surname}`
  56. }
  57. else{return `${this.name} ${this.surname}`}
  58. }
  59. }
  60. return obj
  61. }
  62. const a = createPerson("Вася", "Пупкин")
  63. const b = createPerson("Анна", "Иванова")
  64. const c = createPerson("Елизавета", "Петрова")
  65. console.log(a.getFullName()) //Вася Пупкин
  66. a.fatherName = 'Иванович'
  67. console.log(a.getFullName())//Вася Иванович Пупкин
  68. console.log(b.getFullName()) //Анна Иванова
  69. }
  70. //createPersonClosure
  71. {
  72. function createPersonClosure(name, surname){
  73. let age
  74. let fatherName
  75. let obj
  76. return obj = {
  77. getName: function (){return name},
  78. getSurname: function (){return surname},
  79. getFatherName: function (){return fatherName},
  80. getAge: function (){return age},
  81. getFullName: function (){return `${name} ${surname} ${fatherName} ${age}`},
  82. setName: function (newName){
  83. if(newName.slice(0,1)===newName.toUpperCase().slice(0,1)){
  84. name=newName
  85. return obj.getName()
  86. }
  87. else{return obj.getName()}
  88. },
  89. setSurname: function (newSurname){
  90. if(newSurname.slice(0,1)===newSurname.toUpperCase().slice(0,1)){
  91. surname=newSurname
  92. return obj.getSurname()
  93. }
  94. else{return obj.getSurname()}
  95. },
  96. setFatherName: function (newFatherName){
  97. if(newFatherName.slice(0,1)===newFatherName.toUpperCase().slice(0,1)){
  98. fatherName=newFatherName
  99. return obj.getFatherName()
  100. }
  101. else{return obj.getFatherName()}
  102. },
  103. setAge: function (newAge){
  104. if(newAge<100 && newAge>0){
  105. age=newAge
  106. return obj.getAge()
  107. }
  108. else{return obj.getAge()}
  109. },
  110. setFullName: function (newFullName){
  111. let [surnameCheck, nameCheck, fatherNameCheck] = newFullName.split(' ')
  112. if(nameCheck.slice(0,1)===nameCheck.toUpperCase().slice(0,1) && fatherNameCheck.slice(0,1)===fatherNameCheck.toUpperCase().slice(0,1) && surnameCheck.slice(0,1)===surnameCheck.toUpperCase().slice(0,1)){
  113. [surname, name, fatherName] = newFullName.split(' ')
  114. return obj.getFullName()
  115. }
  116. else{return obj.getFullName()}
  117. }
  118. }
  119. }
  120. const a = createPersonClosure("Вася", "Пупкин")
  121. const b = createPersonClosure("Анна", "Иванова")
  122. console.log(a.getName())
  123. a.setAge(15)
  124. a.setAge(150) //не работает
  125. b.setFullName("Петрова Анна Николаевна")
  126. console.log(b.getFatherName()) //Николаевна
  127. }
  128. //createPersonClosureDestruct
  129. {
  130. function createPersonClosureDestruct(person){
  131. let {name='Иван',surname='Иванов',fatherName='Иванович',age=30}=person
  132. let obj
  133. return obj = {
  134. getName: function (){return name},
  135. getSurname: function (){return surname},
  136. getFatherName: function (){return fatherName},
  137. getAge: function (){return age},
  138. getFullName: function (){return `${name} ${surname} ${fatherName} ${age}`},
  139. setName: function (newName){
  140. if(newName.slice(0,1)===newName.toUpperCase().slice(0,1)){
  141. name=newName
  142. return obj.getName()
  143. }
  144. else{return obj.getName()}
  145. },
  146. setSurname: function (newSurname){
  147. if(newSurname.slice(0,1)===newSurname.toUpperCase().slice(0,1)){
  148. surname=newSurname
  149. return obj.getSurname()
  150. }
  151. else{return obj.getSurname()}
  152. },
  153. setFatherName: function (newFatherName){
  154. if(newFatherName.slice(0,1)===newFatherName.toUpperCase().slice(0,1)){
  155. fatherName=newFatherName
  156. return obj.getFatherName()
  157. }
  158. else{return obj.getFatherName()}
  159. },
  160. setAge: function (newAge){
  161. if(newAge<100 && newAge>0){
  162. age=newAge
  163. return obj.getAge()
  164. }
  165. else{return obj.getAge()}
  166. },
  167. setFullName: function (newFullName){
  168. let [surnameCheck, nameCheck, fatherNameCheck,] = newFullName.split(' ')
  169. if(nameCheck.slice(0,1)===nameCheck.toUpperCase().slice(0,1) && fatherNameCheck.slice(0,1)===fatherNameCheck.toUpperCase().slice(0,1) && surnameCheck.slice(0,1)===surnameCheck.toUpperCase().slice(0,1)){
  170. [fatherName=fatherName, surname=surname, name=name, age=age] = newFullName.split(' ')
  171. return obj.getFullName()
  172. }
  173. else{return obj.getFullName()}
  174. }
  175. }
  176. }
  177. }
  178. //isSorted
  179. {
  180. function isSorted(...arr){
  181. arr = arr.map((a,b)=>a>b?a:false)
  182. for(el of arr){
  183. typeof el === 'number'?el=el:arr=false
  184. }
  185. if(typeof arr==='object'){
  186. arr=true
  187. }
  188. return arr
  189. }
  190. //Test isSorted
  191. let arr = []
  192. let el
  193. while (el=+prompt('Добавьте элемент в массив')) {
  194. arr.push(el)
  195. }
  196. isSorted(arr)
  197. }
  198. //personForm
  199. //personForm в index.html