hw#11.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // createPerson =============================================================================
  2. function createPerson(name,surname){
  3. return{
  4. name: name,
  5. surname: surname,
  6. getFullName: function(){ return `${this.surname} ${this.name} ${this.fatherName ? this.fatherName : ''}` }
  7. }
  8. }
  9. const a = createPerson("Вася", "Пупкин")
  10. const b = createPerson("Анна", "Иванова")
  11. const c = createPerson("Елизавета", "Петрова")
  12. console.log(a.getFullName())
  13. a.fatherName = 'Иванович'
  14. console.log(b.getFullName())
  15. // createPersonClosure =============================================================================
  16. function createPersonClosure(name,surname){
  17. let age
  18. let fatherName
  19. const fullName = `${surname} ${name} ${fatherName}`
  20. const checkData = (data) => {
  21. if(typeof data === 'string' && data.charAt(0) == data.charAt(0).toUpperCase() ){
  22. return data
  23. }
  24. if(typeof data === 'number' && data >=1 && data <=100){
  25. return data
  26. }
  27. }
  28. return {
  29. getName: () => {return name},
  30. getSurname: () => {return surname},
  31. getFatherName: () => {return fatherName},
  32. getFullName: () => {return fullName},
  33. getAge: () => {return age},
  34. setName: (newName) => {checkData(newName) ? name = newName : newName},
  35. setSurname: (newSurname) => {checkData(newSurname) ? surname = newSurname : newSurname},
  36. setFatherName: (newFatherName) => {checkData(newFatherName) ? fatherName = newFatherName : newFatherName},
  37. setFullName: (newFullName) => {
  38. checkData(newFullName.split(' ')[0]) ? surname = newFullName.split(' ')[0] : newFullName
  39. checkData(newFullName.split(' ')[1]) ? name = newFullName.split(' ')[1] : newFullName
  40. checkData(newFullName.split(' ')[2]) ? fatherName = newFullName.split(' ')[2] : newFullName
  41. },
  42. setAge: (newAge) => {checkData(newAge) ? age = newAge : newAge},
  43. }
  44. }
  45. const a = createPersonClosure("Вася", "Пупкин")
  46. const b = createPersonClosure("Анна", "Иванова")
  47. console.log(a.getName())
  48. a.setAge(15)
  49. console.log(a.getAge())
  50. a.setAge(150)
  51. console.log(a.getAge())
  52. b.setFullName("Петрова Анна Николаевна")
  53. console.log(b.getFatherName())
  54. // createPersonClosureDestruct =============================================================================
  55. function createPerson(person){
  56. return{
  57. name: person.split(' ')[0],
  58. surname: person.split(' ')[1],
  59. fatherName: person.split(' ')[2]
  60. }
  61. }
  62. function createPersonClosureDestruct(person){
  63. let {surname = 'Фамилия не задана', name = 'Имя не задано', fatherName = 'Отчество не задано', fullName, age = 'Возраст не задан'} = person
  64. const checkData = (data) => {
  65. if(typeof data === 'string' && data.charAt(0) == data.charAt(0).toUpperCase() ){
  66. return data
  67. }
  68. if(typeof data === 'number' && data >=1 && data <=100){
  69. return data
  70. }
  71. }
  72. return {
  73. getName: () => {return checkData(name)},
  74. getSurname: () => {return checkData(surname)},
  75. getFatherName: () => {return checkData(fatherName)},
  76. getFullName: () => {return fullName = `${surname} ${name} ${fatherName}`},
  77. getAge: () => {return checkData(age)},
  78. setName: (newName) => {checkData(newName) ? name = newName : newName},
  79. setSurname: (newSurname) => {checkData(newSurname) ? surname = newSurname : newSurname},
  80. setFatherName: (newFatherName) => {checkData(newFatherName) ? fatherName = newFatherName : newFatherName},
  81. setFullName: (newFullName) => {
  82. checkData(newFullName.split(' ')[0]) ? surname = newFullName.split(' ')[0] : newFullName
  83. checkData(newFullName.split(' ')[1]) ? name = newFullName.split(' ')[1] : newFullName
  84. checkData(newFullName.split(' ')[2]) ? fatherName = newFullName.split(' ')[2] : newFullName
  85. },
  86. setAge: (newAge) => {checkData(newAge) ? age = newAge : newAge},
  87. }
  88. }
  89. const a = createPersonClosureDestruct(createPerson("Вася Пупкин"))
  90. const b = createPersonClosureDestruct({name: 'Николай', age: 75})
  91. // isSorted =============================================================================
  92. function isSorted (...arg){
  93. for (let i = 0; i < arg.length - 1; i ++){
  94. if( (arg[i] > arg[i+1]) || typeof arg[i] !== 'number' ){
  95. return false
  96. }
  97. }
  98. return true
  99. }
  100. const a = isSorted(1,2,3,4,5,6,7)
  101. console.log(a)
  102. const b = isSorted(1,2,3,4,-5,6,7)
  103. console.log(b)
  104. const c = isSorted(1,2,3,'str',5,6,7)
  105. console.log(c)
  106. // Test isSorted =============================================================================
  107. function isSorted (...arg){
  108. let argNum
  109. while (argNum !== null){
  110. argNum = prompt('Введите числа, которые хотите добавить в массив')
  111. if(argNum){
  112. arg.push(argNum)
  113. }
  114. }
  115. for (let i = 0; i < arg.length - 1; i ++){
  116. if( (arg[i] > arg[i+1]) || typeof arg[i] === 'number' ){
  117. return false
  118. }
  119. }
  120. return true
  121. }
  122. isSorted()
  123. // personForm =============================================================================
  124. function createPerson(person){
  125. return{
  126. name: person.split(' ')[0],
  127. surname: person.split(' ')[1],
  128. fatherName: person.split(' ')[2]
  129. }
  130. }
  131. function createPersonClosureDestruct(person){
  132. let {surname = 'Фамилия не задана', name = 'Имя не задано', fatherName = 'Отчество не задано', fullName, age = 'Возраст не задан'} = person
  133. const checkData = (data) => {
  134. if(typeof data === 'string' && data.charAt(0) == data.charAt(0).toUpperCase()){
  135. return data
  136. }
  137. if(typeof data === 'number' && data >=1 && data <=100){
  138. return data
  139. }
  140. }
  141. return {
  142. getName: () => {return checkData(name)},
  143. getSurname: () => {return checkData(surname)},
  144. getFatherName: () => {return checkData(fatherName)},
  145. getFullName: () => {return fullName = `${surname} ${name} ${fatherName}`},
  146. getAge: () => {return age},
  147. setName: (newName) => {checkData(newName) ? name = newName : name},
  148. setSurname: (newSurname) => {checkData(newSurname) ? surname = newSurname : newSurname},
  149. setFatherName: (newFatherName) => {checkData(newFatherName) ? fatherName = newFatherName : newFatherName},
  150. setFullName: (newFullName) => {
  151. checkData(newFullName.split(' ')[0]) ? surname = newFullName.split(' ')[0] : newFullName
  152. checkData(newFullName.split(' ')[1]) ? name = newFullName.split(' ')[1] : newFullName
  153. checkData(newFullName.split(' ')[2]) ? fatherName = newFullName.split(' ')[2] : newFullName
  154. },
  155. setAge: (newAge) => {checkData(newAge) ? age = newAge : newAge},
  156. }
  157. }
  158. const b = createPersonClosureDestruct("Анна", "Иванова")
  159. b.setAge(15)
  160. b.setFullName("Петрова Анна Николаевна")
  161. function personForm( parent, person ){
  162. const form = document.createElement('form')
  163. parent.append(form)
  164. Object.assign(form.style, {display: 'flex', flexDirection: 'column', width: '300px', backgroundColor: '#a2cac9'})
  165. const inputName = document.createElement('input')
  166. form.appendChild(inputName)
  167. Object.assign(inputName.style, {margin: '10px'})
  168. inputName.placeholder = 'Name'
  169. inputName.value = person.getName()
  170. inputName.oninput = () => {person.setName(inputName.value)}
  171. const inputSurname = document.createElement('input')
  172. form.appendChild(inputSurname)
  173. Object.assign(inputSurname.style, {margin: '10px'})
  174. inputSurname.placeholder = 'Surname'
  175. inputSurname.value = person.getSurname()
  176. inputSurname.oninput = () => person.setSurname(inputSurname.value)
  177. const inputFatherName = document.createElement('input')
  178. form.appendChild(inputFatherName)
  179. Object.assign(inputFatherName.style, {margin: '10px'})
  180. inputFatherName.placeholder = 'Fathername'
  181. inputFatherName.value = person.getFatherName()
  182. inputFatherName.oninput = () => person.setFatherName(inputFatherName.value)
  183. const inputAge = document.createElement('input')
  184. form.appendChild(inputAge)
  185. Object.assign(inputAge.style, {margin: '10px'})
  186. inputAge.placeholder = 'Age'
  187. inputAge.value = person.getAge()
  188. inputAge.oninput = () => {person.setAge(+inputAge.value)}
  189. const inputFullName = document.createElement('input')
  190. form.appendChild(inputFullName)
  191. Object.assign(inputFullName.style, {margin: '10px'})
  192. inputFullName.placeholder = 'Full Name'
  193. inputFullName.value = person.getFullName()
  194. inputFullName.oninput = () => person.setFullName(inputFullName.value)
  195. }
  196. const f = personForm(document.body, b)
  197. // getSetForm =============================================================================
  198. function getSetForm(parent, obj) {
  199. const registry = {}
  200. const objForm = document.createElement('form')
  201. parent.append(objForm)
  202. Object.assign(objForm.style, {display: 'flex', flexDirection: 'column', width: '300px', backgroundColor: '#a2cac9'})
  203. const updateInputs = () => {
  204. for (const registryKey in registry) {
  205. const keyToMethod = `get${registryKey}`
  206. if(obj[keyToMethod]){
  207. registry[registryKey].value = obj[keyToMethod]()
  208. }
  209. }
  210. }
  211. for (const objKey in obj) {
  212. const getOrSet = objKey.slice(0, 3)
  213. const objValue = objKey.slice(3, objKey.length)
  214. const getValue = `get${objValue}`
  215. const setValue = `set${objValue}`
  216. if (registry[objValue] === undefined) registry[objValue] = document.createElement('input')
  217. if (!obj[setValue]) registry[objValue].disabled = true
  218. objForm.appendChild(registry[objValue])
  219. Object.assign(registry[objValue].style, {margin: '10px'})
  220. registry[objValue].type = typeof obj[getValue]()
  221. registry[objValue].placeholder = `${objValue}`
  222. registry[objValue].oninput = () => registry[objValue].type == 'number' ? obj[setValue](+registry[objValue].value) : obj[setValue](registry[objValue].value)
  223. registry[objValue].onchange = () => updateInputs()
  224. }
  225. console.log(updateInputs())
  226. }