123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- //Arrow to Functions
- //Переведите любые пять заданий из предыдущего ДЗ по функциям в синтаксис function
- //Number: temperature
- {
- function fahrenheit(x){return x * 1.8 + 32} // функция конвертации из цельсия в фарингейты
- console.log(fahrenheit(20))//68 градусов по фарингейту
- }
- //Flats
- {
- function flats (apartmentNumber,floorNumber,numberApartmentsFloor){
- let sumApartments = floorNumber*numberApartmentsFloor// здесь мы узнаем сколько квартир в подьезде
- let entrance = Math.ceil(apartmentNumber/sumApartments)//номер подьезда
- let floor = Math.ceil((apartmentNumber%sumApartments)/numberApartmentsFloor)//номер этажа
- return {entrance,floor}
- }
- console.log(flats(65,9,4))//{entrance: 2, floor: 8}
- }
- //Credentials
- {
- function credentials (){
- let name = prompt("Введите имя").trim().toLowerCase();
- let surname = prompt("Введите фамилию").trim().toLowerCase();
- let fatherName = prompt("Введите отчество").trim().toLowerCase();
- name = name[0].toUpperCase()+name.slice(1);
- surname = surname[0].toUpperCase()+surname.slice(1);
- fatherName = fatherName[0].toUpperCase()+fatherName.slice(1);
- fullName = name+' '+surname+' '+fatherName
- return {name,surname,fatherName,fullName}
- }
- console.log(credentials())
- }
- //New line
- {
- function newLine (str){
- let result = (str.split('\\n').join('\n'))
- return result
- }
- console.log(newLine('1 строка\n2 строка'))
- }
- //Prompt OR
- {
- function promptOr(age){
- age ? alert('Вы ' +(2022 - age)+ ' года рождения!') :alert('Ошибка')
- }
- console.log(promptOr(+prompt('Сколько вам лет?')))
- }
- //createPerson
- {
- function createPerson(name, surname){
- let obj = {
- 'name': name,
- 'surname': surname,
- getFullName: function (){
- if(obj.fatherName){
- return `${this.name} ${this.fatherName} ${this.surname}`
- }
- else{return `${this.name} ${this.surname}`}
- }
- }
- return obj
- }
- const a = createPerson("Вася", "Пупкин")
- const b = createPerson("Анна", "Иванова")
- const c = createPerson("Елизавета", "Петрова")
- console.log(a.getFullName()) //Вася Пупкин
- a.fatherName = 'Иванович'
- console.log(a.getFullName())//Вася Иванович Пупкин
- console.log(b.getFullName()) //Анна Иванова
- }
- //createPersonClosure
- {
- function createPersonClosure(name, surname){
- let age
- let fatherName
- let obj
- return obj = {
- getName: function (){return name},
- getSurname: function (){return surname},
- getFatherName: function (){return fatherName},
- getAge: function (){return age},
- getFullName: function (){return `${name} ${surname} ${fatherName} ${age}`},
- setName: function (newName){
- if(newName.slice(0,1)===newName.toUpperCase().slice(0,1)){
- name=newName
- return obj.getName()
- }
- else{return obj.getName()}
- },
- setSurname: function (newSurname){
- if(newSurname.slice(0,1)===newSurname.toUpperCase().slice(0,1)){
- surname=newSurname
- return obj.getSurname()
- }
- else{return obj.getSurname()}
- },
- setFatherName: function (newFatherName){
- if(newFatherName.slice(0,1)===newFatherName.toUpperCase().slice(0,1)){
- fatherName=newFatherName
- return obj.getFatherName()
- }
- else{return obj.getFatherName()}
- },
- setAge: function (newAge){
- if(newAge<100 && newAge>0){
- age=newAge
- return obj.getAge()
- }
- else{return obj.getAge()}
- },
- setFullName: function (newFullName){
- let [surnameCheck, nameCheck, fatherNameCheck] = newFullName.split(' ')
- 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)){
- [surname, name, fatherName] = newFullName.split(' ')
- return obj.getFullName()
- }
- else{return obj.getFullName()}
- }
- }
- }
- const a = createPersonClosure("Вася", "Пупкин")
- const b = createPersonClosure("Анна", "Иванова")
- console.log(a.getName())
- a.setAge(15)
- a.setAge(150) //не работает
- b.setFullName("Петрова Анна Николаевна")
- console.log(b.getFatherName()) //Николаевна
- }
- //createPersonClosureDestruct
- {
- function createPersonClosureDestruct(person){
- let {name='Иван',surname='Иванов',fatherName='Иванович',age=30}=person
- let obj
- return obj = {
- getName: function (){return name},
- getSurname: function (){return surname},
- getFatherName: function (){return fatherName},
- getAge: function (){return age},
- getFullName: function (){return `${name} ${surname} ${fatherName} ${age}`},
- setName: function (newName){
- if(newName.slice(0,1)===newName.toUpperCase().slice(0,1)){
- name=newName
- return obj.getName()
- }
- else{return obj.getName()}
- },
- setSurname: function (newSurname){
- if(newSurname.slice(0,1)===newSurname.toUpperCase().slice(0,1)){
- surname=newSurname
- return obj.getSurname()
- }
- else{return obj.getSurname()}
- },
- setFatherName: function (newFatherName){
- if(newFatherName.slice(0,1)===newFatherName.toUpperCase().slice(0,1)){
- fatherName=newFatherName
- return obj.getFatherName()
- }
- else{return obj.getFatherName()}
- },
- setAge: function (newAge){
- if(newAge<100 && newAge>0){
- age=newAge
- return obj.getAge()
- }
- else{return obj.getAge()}
- },
- setFullName: function (newFullName){
- let [surnameCheck, nameCheck, fatherNameCheck,] = newFullName.split(' ')
- 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)){
- [fatherName=fatherName, surname=surname, name=name, age=age] = newFullName.split(' ')
- return obj.getFullName()
- }
- else{return obj.getFullName()}
- }
- }
- }
- }
- //isSorted
- {
- function isSorted(...arr){
- arr = arr.map((a,b)=>a>b?a:false)
- for(el of arr){
- typeof el === 'number'?el=el:arr=false
- }
- if(typeof arr==='object'){
- arr=true
- }
- return arr
- }
- //Test isSorted
- let arr = []
- let el
- while (el=+prompt('Добавьте элемент в массив')) {
- arr.push(el)
- }
- isSorted(arr)
- }
- //personForm
- //personForm в index.html
|