main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //Arrow to Functions
  2. /*
  3. {
  4. function far (t) {
  5. let result = t * 9/5 + 32
  6. return result
  7. }
  8. console.log(far(1) + " F")
  9. }
  10. {
  11. const red = +prompt ("Write your value for red:")
  12. const green = +prompt ("Write your value for green :")
  13. const blue = +prompt ("Write your value for blue :")
  14. function rgb (r, g, b) {
  15. r = r.toString(16)
  16. if(r.length < 2) {
  17. r = r+r
  18. }
  19. g = g.toString(16)
  20. if(g.length < 2) {
  21. g = g + g
  22. }
  23. b = b.toString(16)
  24. if(b.length < 2) {
  25. b = b + b
  26. }
  27. let result =` #${r+g+b} `
  28. return result
  29. }
  30. console.log(rgb(red,green,blue))
  31. }
  32. {
  33. function func () {
  34. function capitalize (str) {
  35. let result = str.split('')[0].toUpperCase() + str.slice(1,+Infinity).toLowerCase()
  36. return result //именно этот код обеспечит возврат результата функции
  37. }
  38. let firstname = capitalize(prompt("Write your name"))
  39. let surname = capitalize(prompt("Write your surname"))
  40. let fathername = capitalize(prompt("Write your thirdname"))
  41. let fullname = (firstname + " " + surname + " " + fathername)
  42. let result = {firstname, surname, fathername, fullname}
  43. return result
  44. }
  45. console.log(func())
  46. }
  47. {
  48. const persons = [
  49. {name: "Иван", age: 17},
  50. {name: "Мария", age: 35},
  51. {name: "Алексей", age: 73},
  52. {name: "Яков", age: 12},
  53. ]
  54. function funSort () {
  55. function results (arr, name, booln=true){
  56. let result = arr.sort((first,second) => (first[name] < second[name]) === booln ? -1 : 1)
  57. return result
  58. }
  59. return results
  60. }
  61. console.log(funSort()(persons, "age"))
  62. console.log(funSort()(persons, "age", false ))
  63. }
  64. {
  65. const car = {
  66. "Name":"chevrolet chevelle malibu",
  67. "Cylinders":8,
  68. "Displacement":307,
  69. "Horsepower":130,
  70. "Weight_in_lbs":3504,
  71. "Origin":"USA",
  72. "in_production": false
  73. }
  74. function createObject (obj) {
  75. let str = "<form>"
  76. for ( const [key,value] of Object.entries(obj)){
  77. if (typeof(value) === "string"){
  78. str += `<label>${key}: <input type="text" value="${value}" /> </label> `
  79. }
  80. if(typeof(value) === "number"){
  81. str += `<label>${key}: <input type="number" value="${value}" /> </label> `
  82. }
  83. if(typeof(value) === "boolean"){
  84. str += `<label>${key}: <input type="checkbox" value="${value}" /> </label> `
  85. }
  86. }
  87. str += "</form>"
  88. return document.write(str)
  89. }
  90. console.log(createObject(car))
  91. }
  92. //createPerson
  93. //Создайте функцию createPerson, которая принимает два параметра: name и surname,
  94. //и возвращает объект с ключами name, surname, getFullName. getFullName должна быть функцией,
  95. //которая работает с объектом через this,
  96. //а так же готова к тому, что в объекте в последствии добавить ключ fatherName
  97. {
  98. function createPerson(name, surname) {
  99. let obj = { "name":name,
  100. fatherName:"",
  101. "surname":surname,
  102. getFullName:function (){
  103. return `${this.name} ${this.fatherName} ${this.surname} `
  104. }
  105. }
  106. return obj
  107. }
  108. const a = createPerson("Вася", "Пупкин")
  109. const b = createPerson("Анна", "Иванова")
  110. const c = createPerson("Елизавета", "Петрова")
  111. console.log(a) //Вася Пупкин
  112. a.fatherName = 'Иванович'
  113. c.fatherName = "Олеговна"
  114. console.log(a.getFullName()) //Вася Иванович Пупкин
  115. console.log(b.getFullName()) //Анна Иванова
  116. console.log(c.getFullName())
  117. }
  118. //createPersonClosure
  119. {
  120. function createPersonClosure (name="Василь",surname="Василич") {
  121. let age=20
  122. let fatherName="Васильович"
  123. let result
  124. return result = {
  125. getName: function (){return name},
  126. getSurName : function (){return surname},
  127. getFatherName : function (){return fatherName},
  128. getAge : function(){return age},
  129. getFullName : function(){
  130. return `${name} ${surname} ${fatherName}`
  131. },
  132. setName : function (newName){
  133. if(newName.slice(0,1) === newName.toUpperCase().slice(0,1)){
  134. name = newName
  135. return result.getName()
  136. }
  137. else return result.getName()
  138. },
  139. setSurName: function(newSurName){
  140. if(newSurName.slice(0,1) === newSurName.toUpperCase().slice(0,1)){
  141. surname = newSurName
  142. return result.getSurName()
  143. }
  144. else return result.getSurName()
  145. },
  146. setFatherName: function(newFatherName){
  147. if(newFatherName.slice(0,1) === newFatherName.toUpperCase().slice(0,1)){
  148. fatherName = newFatherName
  149. return result.getFatherName()
  150. }
  151. else return result.getFatherName()
  152. },
  153. setAge: function(newAge){
  154. if(newAge >= 0 && newAge < 101){
  155. age = newAge
  156. return result.getAge()
  157. }
  158. else return result.getAge()
  159. },
  160. setFullName: function(newFullName){
  161. let [reName,reSurName,reFatherName] = newFullName.split(" ")
  162. if(reName.slice(0,1) === reName.toUpperCase().slice(0,1) && reSurName.slice(0,1) === reSurName.toUpperCase().slice(0,1) && reFatherName.slice(0,1) === reFatherName.toUpperCase().slice(0,1)){
  163. [name,surname,fatherName] = newFullName.split(" ")
  164. return result.getFullName()
  165. }
  166. else return result.getFullName()
  167. }
  168. }
  169. }
  170. const a = createPersonClosure("Вася", "Пупкин")
  171. const b = createPersonClosure("Анна", "Иванова")
  172. console.log(a.getName())
  173. console.log(a.setAge(15))
  174. console.log(a.setAge(150)) //не работает
  175. b.setFullName("Петрова Анна Николаевна")
  176. console.log(b.getFatherName()) //Николаевна
  177. }
  178. //createPersonClosureDestruct
  179. {
  180. function createPerson(name, surname) {
  181. let obj = { "name":name,
  182. fatherName:"",
  183. "surname":surname,
  184. getFullName:function (){
  185. return `${this.name} ${this.surname} ${this.fatherName} `
  186. }
  187. }
  188. return obj
  189. }
  190. function createPersonClosureDestruct (person) {
  191. let {name='Василь',surname='Васильов',fatherName='Иванович',age=20} = person
  192. let result
  193. return result = {
  194. getName: function (){return name},
  195. getSurName : function (){return surname},
  196. getFatherName : function (){return fatherName},
  197. getAge : function(){return age},
  198. getFullName : function(){
  199. return `${name} ${surname} ${fatherName}`
  200. },
  201. setName : function (newName){
  202. if(newName.slice(0,1) === newName.toUpperCase().slice(0,1)){
  203. name = newName
  204. return result.getName()
  205. }
  206. else return result.getName()
  207. },
  208. setSurName: function(newSurName){
  209. if(newSurName.slice(0,1) === newSurName.toUpperCase().slice(0,1)){
  210. surname = newSurName
  211. return result.getSurName()
  212. }
  213. else return result.getSurName()
  214. },
  215. setFatherName: function(newFatherName){
  216. if(newFatherName.slice(0,1) === newFatherName.toUpperCase().slice(0,1)){
  217. fatherName = newFatherName
  218. return result.getFatherName()
  219. }
  220. else return result.getFatherName()
  221. },
  222. setAge: function(newAge){
  223. if(newAge >= 0 && newAge < 101){
  224. age = newAge
  225. return result.getAge()
  226. }
  227. else return result.getAge()
  228. },
  229. setFullName: function(newFullName){
  230. let [reName,reSurName,reFatherName] = newFullName.split(" ")
  231. if(reName.slice(0,1) === reName.toUpperCase().slice(0,1) && reSurName.slice(0,1) === reSurName.toUpperCase().slice(0,1) && reFatherName.slice(0,1) === reFatherName.toUpperCase().slice(0,1)){
  232. [name,surname,fatherName] = newFullName.split(" ")
  233. return result.getFullName()
  234. }
  235. else return result.getFullName()
  236. }
  237. }
  238. }
  239. const a = createPersonClosureDestruct(createPerson("Вася Пупкин"))
  240. const b = createPersonClosureDestruct({name: 'Николай', age: 75})
  241. console.log(a.getName())
  242. console.log(b.getAge())
  243. }
  244. //isSorted + Test
  245. {
  246. let arr = []
  247. let question
  248. while(question = +prompt("Write your element for array")){
  249. arr.push(question)
  250. }
  251. console.log(arr)
  252. function isSorted (...params){
  253. let firstNumber = -Infinity
  254. for (x of params){
  255. if(typeof(x) === "number" && firstNumber<x ){
  256. firstNumber = x
  257. }
  258. else return false
  259. }
  260. return true
  261. }
  262. console.log(isSorted(...arr))
  263. }
  264. */
  265. //personForm
  266. {
  267. function createPersonClosure (name="Василь",surname="Василич") {
  268. let age=20
  269. let fatherName=" Пупкин"
  270. let result
  271. return result = {
  272. getName: function (){return name},
  273. getSurName : function (){return surname},
  274. getFatherName : function (){return fatherName},
  275. getAge : function(){return age},
  276. getFullName : function(){
  277. return `${name} ${surname} ${fatherName}`
  278. },
  279. setName : function (newName){
  280. if(newName.slice(0,1) === newName.toUpperCase().slice(0,1)){
  281. name = newName
  282. return result.getName()
  283. }
  284. else return result.getName()
  285. },
  286. setSurName: function(newSurName){
  287. if(newSurName.slice(0,1) === newSurName.toUpperCase().slice(0,1)){
  288. surname = newSurName
  289. return result.getSurName()
  290. }
  291. else return result.getSurName()
  292. },
  293. setFatherName: function(newFatherName){
  294. if(newFatherName.slice(0,1) === newFatherName.toUpperCase().slice(0,1)){
  295. fatherName = newFatherName
  296. return result.getFatherName()
  297. }
  298. else return result.getFatherName()
  299. },
  300. setAge: function(newAge){
  301. if(newAge >= 0 && newAge < 101){
  302. age = newAge
  303. return result.getAge()
  304. }
  305. else return result.getAge()
  306. },
  307. setFullName: function(newFullName){
  308. let [reName,reSurName,reFatherName] = newFullName.split(" ")
  309. if(reName.slice(0,1) === reName.toUpperCase().slice(0,1) && reSurName.slice(0,1) === reSurName.toUpperCase().slice(0,1) && reFatherName.slice(0,1) === reFatherName.toUpperCase().slice(0,1)){
  310. [name,surname,fatherName] = newFullName.split(" ")
  311. return result.getFullName()
  312. }
  313. else return result.getFullName()
  314. }
  315. }
  316. }
  317. const b = createPersonClosure("Анна", "Иванова")
  318. console.log(b.setAge(15))
  319. console.log(b.setFullName("Анна Николаевна Петрова"))
  320. function personForm(parent, person){
  321. let inputName = document.createElement("input")
  322. let inputSurName = document.createElement("input")
  323. let inputFatherName = document.createElement("input")
  324. let inputAge = document.createElement("input")
  325. let inputFullName = document.createElement("input")
  326. inputName.value = person.getName()
  327. inputSurName.value = person.getSurName()
  328. inputFatherName.value = person.getFatherName()
  329. inputAge.value = person.getAge()
  330. inputFullName.value = person.getFullName()
  331. inputFullName.style.width = " 190px"
  332. parent.append(inputName)
  333. parent.append(inputSurName)
  334. parent.append(inputFatherName)
  335. parent.append(inputAge)
  336. parent.append(inputFullName)
  337. inputName.oninput = () => {
  338. inputName.value = person.setName(inputName.value)
  339. }
  340. inputSurName.oninput = () => {
  341. inputSurName.value = person.setSurName(inputSurName.value)
  342. }
  343. inputFatherName.oninput = () => {
  344. inputFatherName.value = person.setFatherName(inputFatherName.value)
  345. }
  346. inputAge.oninput = () => {
  347. inputAge.value = person.setAge(inputAge.value)
  348. }
  349. inputFullName.oninput = () => {
  350. inputFullName.value = person.setFullName(inputFullName.value)
  351. }
  352. }
  353. let form = document.createElement("form")
  354. document.body.append(form)
  355. personForm(form,b)
  356. }