123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- //Arrow to Functions
- /*
- {
- function far (t) {
- let result = t * 9/5 + 32
- return result
- }
- console.log(far(1) + " F")
- }
- {
- const red = +prompt ("Write your value for red:")
- const green = +prompt ("Write your value for green :")
- const blue = +prompt ("Write your value for blue :")
- function rgb (r, g, b) {
- r = r.toString(16)
- if(r.length < 2) {
- r = r+r
- }
- g = g.toString(16)
- if(g.length < 2) {
- g = g + g
- }
- b = b.toString(16)
- if(b.length < 2) {
- b = b + b
- }
- let result =` #${r+g+b} `
- return result
-
- }
-
- console.log(rgb(red,green,blue))
- }
- {
- function func () {
- function capitalize (str) {
- let result = str.split('')[0].toUpperCase() + str.slice(1,+Infinity).toLowerCase()
- return result //именно этот код обеспечит возврат результата функции
- }
-
- let firstname = capitalize(prompt("Write your name"))
- let surname = capitalize(prompt("Write your surname"))
- let fathername = capitalize(prompt("Write your thirdname"))
- let fullname = (firstname + " " + surname + " " + fathername)
-
- let result = {firstname, surname, fathername, fullname}
- return result
- }
-
- console.log(func())
- }
- {
- const persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
-
- function funSort () {
- function results (arr, name, booln=true){
- let result = arr.sort((first,second) => (first[name] < second[name]) === booln ? -1 : 1)
- return result
- }
- return results
- }
-
- console.log(funSort()(persons, "age"))
- console.log(funSort()(persons, "age", false ))
- }
- {
- const car = {
- "Name":"chevrolet chevelle malibu",
- "Cylinders":8,
- "Displacement":307,
- "Horsepower":130,
- "Weight_in_lbs":3504,
- "Origin":"USA",
- "in_production": false
- }
-
- function createObject (obj) {
- let str = "<form>"
- for ( const [key,value] of Object.entries(obj)){
- if (typeof(value) === "string"){
- str += `<label>${key}: <input type="text" value="${value}" /> </label> `
- }
- if(typeof(value) === "number"){
- str += `<label>${key}: <input type="number" value="${value}" /> </label> `
- }
- if(typeof(value) === "boolean"){
- str += `<label>${key}: <input type="checkbox" value="${value}" /> </label> `
- }
-
-
- }
- str += "</form>"
-
- return document.write(str)
-
- }
-
- console.log(createObject(car))
-
- }
- //createPerson
- //Создайте функцию createPerson, которая принимает два параметра: name и surname,
- //и возвращает объект с ключами name, surname, getFullName. getFullName должна быть функцией,
- //которая работает с объектом через this,
- //а так же готова к тому, что в объекте в последствии добавить ключ fatherName
- {
- function createPerson(name, surname) {
-
- let obj = { "name":name,
- fatherName:"",
- "surname":surname,
- getFullName:function (){
- return `${this.name} ${this.fatherName} ${this.surname} `
- }
-
- }
-
- return obj
- }
-
- const a = createPerson("Вася", "Пупкин")
- const b = createPerson("Анна", "Иванова")
- const c = createPerson("Елизавета", "Петрова")
- console.log(a) //Вася Пупкин
- a.fatherName = 'Иванович'
- c.fatherName = "Олеговна"
- console.log(a.getFullName()) //Вася Иванович Пупкин
- console.log(b.getFullName()) //Анна Иванова
- console.log(c.getFullName())
- }
- //createPersonClosure
- {
- function createPersonClosure (name="Василь",surname="Василич") {
- let age=20
- let fatherName="Васильович"
- let result
- return result = {
- getName: function (){return name},
- getSurName : function (){return surname},
- getFatherName : function (){return fatherName},
- getAge : function(){return age},
- getFullName : function(){
- return `${name} ${surname} ${fatherName}`
- },
- setName : function (newName){
- if(newName.slice(0,1) === newName.toUpperCase().slice(0,1)){
- name = newName
- return result.getName()
- }
- else return result.getName()
- },
- setSurName: function(newSurName){
- if(newSurName.slice(0,1) === newSurName.toUpperCase().slice(0,1)){
- surname = newSurName
- return result.getSurName()
- }
- else return result.getSurName()
- },
- setFatherName: function(newFatherName){
- if(newFatherName.slice(0,1) === newFatherName.toUpperCase().slice(0,1)){
- fatherName = newFatherName
- return result.getFatherName()
- }
- else return result.getFatherName()
- },
- setAge: function(newAge){
- if(newAge >= 0 && newAge < 101){
- age = newAge
- return result.getAge()
- }
- else return result.getAge()
- },
- setFullName: function(newFullName){
- let [reName,reSurName,reFatherName] = newFullName.split(" ")
- 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)){
- [name,surname,fatherName] = newFullName.split(" ")
- return result.getFullName()
- }
- else return result.getFullName()
- }
-
- }
-
- }
- const a = createPersonClosure("Вася", "Пупкин")
- const b = createPersonClosure("Анна", "Иванова")
- console.log(a.getName())
- console.log(a.setAge(15))
- console.log(a.setAge(150)) //не работает
- b.setFullName("Петрова Анна Николаевна")
- console.log(b.getFatherName()) //Николаевна
-
- }
- //createPersonClosureDestruct
- {
- function createPerson(name, surname) {
-
- let obj = { "name":name,
- fatherName:"",
- "surname":surname,
- getFullName:function (){
- return `${this.name} ${this.surname} ${this.fatherName} `
- }
-
- }
-
- return obj
- }
- function createPersonClosureDestruct (person) {
-
- let {name='Василь',surname='Васильов',fatherName='Иванович',age=20} = person
- let result
- return result = {
- getName: function (){return name},
- getSurName : function (){return surname},
- getFatherName : function (){return fatherName},
- getAge : function(){return age},
- getFullName : function(){
- return `${name} ${surname} ${fatherName}`
- },
- setName : function (newName){
- if(newName.slice(0,1) === newName.toUpperCase().slice(0,1)){
- name = newName
- return result.getName()
- }
- else return result.getName()
- },
- setSurName: function(newSurName){
- if(newSurName.slice(0,1) === newSurName.toUpperCase().slice(0,1)){
- surname = newSurName
- return result.getSurName()
- }
- else return result.getSurName()
- },
- setFatherName: function(newFatherName){
- if(newFatherName.slice(0,1) === newFatherName.toUpperCase().slice(0,1)){
- fatherName = newFatherName
- return result.getFatherName()
- }
- else return result.getFatherName()
- },
- setAge: function(newAge){
- if(newAge >= 0 && newAge < 101){
- age = newAge
- return result.getAge()
- }
- else return result.getAge()
- },
- setFullName: function(newFullName){
- let [reName,reSurName,reFatherName] = newFullName.split(" ")
- 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)){
- [name,surname,fatherName] = newFullName.split(" ")
- return result.getFullName()
- }
- else return result.getFullName()
- }
-
- }
-
- }
- const a = createPersonClosureDestruct(createPerson("Вася Пупкин"))
- const b = createPersonClosureDestruct({name: 'Николай', age: 75})
- console.log(a.getName())
- console.log(b.getAge())
- }
- //isSorted + Test
- {
- let arr = []
- let question
- while(question = +prompt("Write your element for array")){
- arr.push(question)
- }
- console.log(arr)
- function isSorted (...params){
- let firstNumber = -Infinity
- for (x of params){
- if(typeof(x) === "number" && firstNumber<x ){
- firstNumber = x
- }
- else return false
-
- }
- return true
-
- }
- console.log(isSorted(...arr))
- }
- */
- //personForm
- {
- function createPersonClosure (name="Василь",surname="Василич") {
- let age=20
- let fatherName=" Пупкин"
- let result
- return result = {
- getName: function (){return name},
- getSurName : function (){return surname},
- getFatherName : function (){return fatherName},
- getAge : function(){return age},
- getFullName : function(){
- return `${name} ${surname} ${fatherName}`
- },
- setName : function (newName){
- if(newName.slice(0,1) === newName.toUpperCase().slice(0,1)){
- name = newName
- return result.getName()
- }
- else return result.getName()
- },
- setSurName: function(newSurName){
- if(newSurName.slice(0,1) === newSurName.toUpperCase().slice(0,1)){
- surname = newSurName
- return result.getSurName()
- }
- else return result.getSurName()
- },
- setFatherName: function(newFatherName){
- if(newFatherName.slice(0,1) === newFatherName.toUpperCase().slice(0,1)){
- fatherName = newFatherName
- return result.getFatherName()
- }
- else return result.getFatherName()
- },
- setAge: function(newAge){
- if(newAge >= 0 && newAge < 101){
- age = newAge
- return result.getAge()
- }
- else return result.getAge()
- },
- setFullName: function(newFullName){
- let [reName,reSurName,reFatherName] = newFullName.split(" ")
- 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)){
- [name,surname,fatherName] = newFullName.split(" ")
- return result.getFullName()
- }
- else return result.getFullName()
- }
-
- }
-
- }
- const b = createPersonClosure("Анна", "Иванова")
- console.log(b.setAge(15))
- console.log(b.setFullName("Анна Николаевна Петрова"))
- function personForm(parent, person){
- let inputName = document.createElement("input")
- let inputSurName = document.createElement("input")
- let inputFatherName = document.createElement("input")
- let inputAge = document.createElement("input")
- let inputFullName = document.createElement("input")
- inputName.value = person.getName()
- inputSurName.value = person.getSurName()
- inputFatherName.value = person.getFatherName()
- inputAge.value = person.getAge()
- inputFullName.value = person.getFullName()
- inputFullName.style.width = " 190px"
- parent.append(inputName)
- parent.append(inputSurName)
- parent.append(inputFatherName)
- parent.append(inputAge)
- parent.append(inputFullName)
- inputName.oninput = () => {
- inputName.value = person.setName(inputName.value)
- }
- inputSurName.oninput = () => {
- inputSurName.value = person.setSurName(inputSurName.value)
- }
- inputFatherName.oninput = () => {
- inputFatherName.value = person.setFatherName(inputFatherName.value)
- }
- inputAge.oninput = () => {
- inputAge.value = person.setAge(inputAge.value)
- }
- inputFullName.oninput = () => {
- inputFullName.value = person.setFullName(inputFullName.value)
- }
-
- }
- let form = document.createElement("form")
- document.body.append(form)
- personForm(form,b)
- }
-
|