//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("
красный
")
document.write("черный
")
}
if(color === "blue" || color === "green"){
document.write("синий
")
document.write("зеленый
")
}
if(!color){
document.write("Я не понял
")
}}
//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)
})