123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- //Number: temperature
- let fahrenheit = x => x * 1.8 + 32 // функция конвертации из цельсия в фарингейты
- console.log(fahrenheit(20))//68 градусов по фарингейту
- //Flats
- let 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
- {
- let 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
- {
- let newLine = (str)=>{
- let result = (str.split('\\n').join('\n'))
- return result
- }
- console.log(newLine('1 строка \n 2 строка'))
- }
- //Prompt OR
- {
- let promptOr = (age) => age ? alert('Вы ' +(2022 - age)+ ' года рождения!') :alert('Ошибка')
- console.log(promptOr(+prompt('Сколько вам лет?')))
- }
- //Login And Password
- {
- let loginAndPassword = (login, pass) => login === 'admin' && pass === 'qwerty' ? console.log(true) : console.log(false)
- console.log(loginAndPassword('admin','qwerty'))//true
- }
- //For Table
- {
- const arr = [[0,0,0,0,0],[0,1,2,3,4],[0,2,4,6,8],[0,3,6,9,12],[0,4,8,12,16]]
- let table = (array) => {
- let str = "<table border='1'>"
- for (let currency of arr){
- str += '<tr>'
- for (let letter of currency){
- str += '<td>'+letter+'</td>'
- }
- str+= '</tr>'
- }
- str+= "</table>"
- return str
- }
- document.write(table(arr))
- }
- //Filter Lexics
- {
- let func = (str,motherWordArr)=>{
- str = str.split(' ')
- let result = str.filter(element => motherWordArr.indexOf(element)=== -1)
- result = result.join(' ')
- return result
- }
- console.log(func('Функция должна возвращать бляха муха строку!',['бляха', 'муха', "пляха", "шабля"]))
- }
- //Currency Table
- {
- let func = ()=>{
- fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
- .then(data => {
- let dataObj = data.rates
- let arrValues = Object.values(dataObj)
- let arrKeys = Object.keys(dataObj)
- let arrData = []
- let i = 0
- for(let valuesEl of arrValues){
- arrData.push(arrValues.map(x=>(valuesEl/x).toFixed(3)))
- arrData[i].unshift(arrKeys[i])
- i++
- }
- arrKeys.unshift('')
- arrData.unshift(arrKeys)
- let currencyTable = "<table border='1'>"
- for (let arrEl of arrData){
- currencyTable += '<tr>'
- for (let cell of arrEl){
- currencyTable += '<td>'+cell+'</td>'
- }
- currencyTable+= '</tr>'
- }
- currencyTable+= "</table>"
- document.write(currencyTable)
- })
- }
- console.log(func())
- }
- //Form
- {
- let func = (obj) => {
- let keysArrCar = Object.keys(obj)
- let valuesArrCar = Object.values(obj)
- let i = 0
- let strType
- let formObjCar = '<form>\n'
- for (let a of keysArrCar){
- if (typeof valuesArrCar[i]=='string'){
- strType = 'text'
- }
- else if(typeof valuesArrCar[i]=='boolean'){
- strType = 'checkbox'
- }
- else{strType=typeof valuesArrCar[i]}
- formObjCar += ' <label>'+a+': <input type="'+strType+'" value="'+valuesArrCar[i]+'"/></label>\n'
- i++
- }
- formObjCar += '</form>'
- document.write(formObjCar)
- }
- const car = {
- "Name":"chevrolet chevelle malibu",
- "Cylinders":8,
- "Displacement":307,
- "Horsepower":130,
- "Weight_in_lbs":3504,
- "Origin":"USA",
- "in_production": false
- }
- console.log(func(car))
- }
- //Array of objects sort
- {
- let sort = (arrObj,key,revers)=>{
- return arrObj.sort((firstEl, secondEl) =>{
- if (typeof firstEl[key] === 'string' ){
- return revers===undefined || revers===true?firstEl[key].localeCompare(secondEl[key]):secondEl[key].localeCompare(firstEl[key])
- }
- else if (typeof firstEl[key] === 'number'){
- return revers===undefined || revers===true?firstEl[key] - secondEl[key]:secondEl[key]-firstEl[key]
- }
- })
- }
- var persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
- //console.log(sort(persons, "age"))
- console.log(sort(persons, "name",false))
- }
- //Table
- {
- let func = (arrayObj,keyObj,revers)=>{
- let sort = (arrObj,key,revers)=>{
- return arrObj.sort((firstEl, secondEl) =>{
- if (typeof firstEl[key] === 'string' ){
- return revers===undefined || revers===true?firstEl[key].localeCompare(secondEl[key]):secondEl[key].localeCompare(firstEl[key])
- }
- else if (typeof firstEl[key] === 'number'){
- return revers===undefined || revers===true?firstEl[key] - secondEl[key]:secondEl[key]-firstEl[key]
- }
- })
- }
- let [...arrCopi] = arrayObj
- persons= sort(arrCopi,keyObj,revers)
- let i = 0
- let arrKeys = []
- for (let x of persons){
- arrKeys += Object.keys(persons[i])+','
- i++
- }
- arrKeys = arrKeys.split(',').slice(0,-1)
- const makeUniq = (arr) => {
- return arr.filter((el1, el2) => arr.indexOf(el1) === el2);
- }
- arrKeys = makeUniq(arrKeys)
- let arrValue = []
- for (let objPersons of persons){
- arrValue.push(arrKeys.map(el=>objPersons[el]===undefined?objPersons[el]=' ':objPersons[el]))
- }
- arrValue.unshift(arrKeys)
-
- let arrTable = "<table border='1'>"
- for (let arrEl of arrValue){
- arrTable += '<tr>'
- for (let cell of arrEl){
- arrTable += '<td>'+cell+'</td>'
- }
- arrTable+= '</tr>'
- }
- arrTable+= "</table>"
- document.write(arrTable)
- }
- const pers = [
- {
- name: 'Мария',
- fatherName: 'Ивановна',
- surname: 'Иванова',
- sex: 'female'
- },
- {
- name: 'Николай',
- fatherName: 'Иванович',
- surname: 'Иванов',
- age: 15
- },
- {
- name: 'Петр',
- fatherName: 'Иванович',
- surname: 'Иванов',
- married: true
- },
- ]
- console.log(func(pers,'surname'))
- }
|