|
@@ -0,0 +1,252 @@
|
|
|
+//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'))
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|