123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- //blocks
- {
- let a = 10
- {
- let b = 20
- {
- let c = 30
- // a=100, b=21, c=30
-
-
- b++
- a *= 10
- console.log(a,b,c)
- }
- {
- let c = 50
- // a=10, b=521, c=50
-
- b += 500
- console.log(a,b,c)
- }
- {
- const a = 100500
- const d = "value"
- // a=100500, b=521, c=undefined, d="value"
- console.log(a,b,d)
- {
- let a = -50
- b = 1000
- // a=-50, b=1000, c=undefined, d="value"
- console.log(a,b,d)
- }
- // a=100500, b=1000, c=undefined, d="value"
- console.log(a,b,d)
- }
- // a=10, b=1000, c=undefined, d=undefined
- console.log(a,b)
- }
- //a=100, b=undefined, c=undefined, d=undefined
- console.log(a)
- //comparison if
- var age = +prompt("Сколько вам лет?","")
- if(age < 0){
- alert("Не бреши!")
- }
- else { if (age < 18){
- alert("школьник")
- }
- else { if (age > 18 && age < 30){
- alert("молодеж")
- }
- else {if (age > 30 && age < 45){
- alert("зрелость")
- }
- else {if (age > 45 && age < 60){
- alert("закат")
- }
- else {if (age > 60){
- alert("как пенсия?")
- }
- else {
- alert("то ли киборг, то ли KERNESS")
- }
- }
- }
- }
- }
- }
- }
- //switch: sizes
- {
- let size = +prompt("Write your size of outerwear?(In USA)") + 2
- let sizeSecond = confirm("Обхват вашей талии 63 - 65 см?") && confirm("Обхват ваших бедер 89-92 см?")
- switch(size){
- case 6 :
- alert(`Your size in UK: ${size}`)
- break ;
- case 8 :
- alert(`Your size in UK: ${size}`)
- break ;
- case 10 :
- alert(`Your size in UK: ${size}`)
- break ;
- case 12 :
- alert(`Your size in UK: ${size}`)
- break ;
- case 14 :
- alert(`Your size in UK: ${size}`)
- break ;
- case 16 :
- alert(`Your size in UK: ${size}`)
- break ;
- case 18 :
- alert(`Your size in UK: ${size}`)
- break ;
-
- }
- switch(sizeSecond){
- case true :
- alert("Ваш размер XS")
- break;
- case false :
- alert("Ваш размер больше XS")
- break;
- }
- }
-
- //switch: if
- {
- let color = prompt("Введите цвет","")
- if(color === "red" || color ==="black"){
- document.write("<div style='background-color: red;'>красный</div>")
- document.write("<div style='background-color: black; color: white;'>черный</div>")
- }
- if(color === "blue" || color === "green"){
- document.write("<div style='background-color: blue;'>синий</div>")
- document.write("<div style='background-color: green;'>зеленый</div>")
- }
- if(!color){
- document.write("<div style='background-color: gray;'>Я не понял</div>")
- }}
- //noswitch
- {
- const drink = prompt("Что вы любите пить")
- const noSwitch = (key, cases, defaultKey='default') => {
- if(cases[key]){
- cases[key]()
- }
- else {
- cases[defaultKey]()
- }
-
-
- }
-
- noSwitch(drink, {
- воду: () => console.log('Самый здоровый выбор!'),
- чай(){
- console.log('Вкусная и полезная штука. Не переусердствуйте с сахаром')
- },
- "пиво": () => console.log('Хорошо летом, да в меру'),
- виски: function(){
- console.log('Да вы, батенька, эстет! Не забудьте лед и сигару')
- },
- default(){
- console.log('шото я не понял')
- }
- })
- }
- //closure calc
-
- fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
- .then(data => {
- const objOfCurrency = Object.entries(data.rates)
- for (let [key,value] of objOfCurrency){
- const button = document.createElement('button')
- button.innerHTML = `${key}`
- document.body.append(button)
- button.onclick = () => {
- const sum = +prompt("Write sum for change: ")
- const result = (sum / value).toFixed(2)
- alert(`You get ${result}$`)
- }
- }
-
- console.log(data)
- })
- //closure calc 2
- {
- fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
- .then(data => {
- const obj = data.rates
- let rate = {}
- for (let key in obj) {
- let option = document.createElement("option")
- option.innerHTML = key
- from.append(option)
-
- option = document.createElement("option")
- option.innerHTML = key
- to.append(option)
-
- let rateOfKeys = {}
- rate[key] = rateOfKeys
- for (let rateKey in obj) {
- rateOfKeys[rateKey] = obj[rateKey] / obj[key]
- }
- }
- const funcResult = () => {
- const fromValue = from.value
- const toValue = to.value
- const amountValue = amount.value
- let rates = rate[fromValue][toValue]
- result.innerHTML =`Your result is: ${amountValue * rates}`
- rate.innerHTML = amountValue
-
- }
- from.onchange = funcResult
- to.onchange = funcResult
- amount.oninput = funcResult
- })
- }
- //countries and cities
- fetch('https://raw.githubusercontent.com/russ666/all-countries-and-cities-json/master/countries.min.json').then(res => res.json())
- .then(data => {
- for(country in data){
- let option = document.createElement("option")
- option.innerHTML = country
- countries.append(option)
- }
- const funcCity = () => {
- cities.innerHTML = ""
-
- for(city of data[countries.value]) {
- let option = document.createElement("option")
- option.innerHTML = city
- cities.append(option)
- }
- }
-
- countries.onchange = funcCity
- funcCity()
-
- console.log(data)
- })
|