//assign: evaluation 
var a = 5;
var b, c;

c = (b = (a * 5) / 2);
// b = (a * 5);
// b = (c = b/2);
// a = 5
// b = 12,5
// c = 12,5

// forgot one semicolon!
// for(let i = 0 i < 5; i++) {
//     console.log(i)
// }

// wrong usage of semicolon
//let a; b = 5;
//b = 5 c = b + 1;

//semicolon logic errors
// if(a === 5) {
//     console.log('FIVE!')
// }else; console.log('not five')

//Number: age
function getAge(age, currentYear = new Date().getFullYear()) {
    try { 
        age = Math.trunc(Number(age))
        if(!age || isNaN(age) || age > 200 || age < 0) {
            throw e
        } else {
            currentYear = currentYear - age
            return [currentYear, currentYear-1]
        }
    } catch(e) {
        return 0
    }
}

let year = getAge(prompt("input your age",""))
if(year) {
    alert(`Looks like you've been born in ${year[0]} or ${year[1]}`)
} else {
    alert('whoops, looks like invalid input value has been given!')
}

//Number: temperature
try {
    let grad = Number(prompt("Input temperature",""))
    let sys = prompt(`
        Type:
            F - to translate to Fahrenheit
            C - to translate to Celsius
    `)
    let res = getTemperature(grad, sys)
    if(res) {
        alert(`${grad}${sys.toLowerCase() === 'c'? '°F':'°C'} = ${res}${sys.toLowerCase() === 'c'? '°C':'°F'}`)
    } else {
        throw e;
    }
} catch(e) {
    alert('invalid input!')
}

function getTemperature(value, system="c") {
    if(isNaN(Number(value))) return false

    switch(system.toLowerCase()) {
        case 'f':
            return ((value * 9/5) + 32).toFixed(2)
        case 'c':
            return ((value - 32) * 5/9).toFixed(2)
        default:
            return false
    }
}

//Number: divide
function flooredDivide(num1, num2) {
    console.log(num1 / num2)
    return Math.floor(num1 / num2)
}

//Number: odd
function oddInformer() {
    let num = prompt("input a number:", "")
    if(isNaN(+num) || !num) {
        alert("not a number!!!")
    } else {
        if(num % 2 !== 0) {
            alert(`${num} is odd`)
        } else {
            alert(`${num} is even`)
        }
    }
}

//String: greeting
//greet user using alert() and prompt()
function greeting() {
    let name = prompt("Hey user, what's your name")
    alert(`Hello, ${name? name : 'Namless One...'}`)
}


//String: lexis 
function lexis(string, word) {
    if(string.indexOf(word) > 0) {
        return true
    } else {
        return false
    }
}

//confirm возвращает true либо false по нажатию на кнопки 'Ок' или 'Отмена' соответственно.

//Boolean
let quest1 = confirm('are you a human being?')
let quest2 = confirm('can you fly?')

if(quest1) {
    if(quest2) {
        alert('wow, you are Superman!')    
    } else {
        alert('Congrats, you are a real human being...')
    }
} else {
    if(quest2) {
        alert('so u must be a bird or smth :/')
    } else {
        alert('is machine spirit talking to me?')
    }
}
//Array: real
let inMyBag = ['shoes', 'shorts', 'shirt', 'laptop', 'charger', 'copybook', 'pen', 'water-bottle']
//Array: booleans
let answers = [quest1, quest2]

//Array: plus
let plus = [1, 2]
plus[2] = plus[0] + plus[1]
console.log(plus)

//Array: plus string
//возможность можно использовать что бы создавать новые строки из множества строчных значений
let plusStr = ['hello', 'My', 'World',]
plusStr[plusStr.length] = plusStr[0] + plusStr[1] + plusStr[2]
console.log(plusStr)

// Object: real
let keyboard = {
    model: 'sven slim 303',
    type: 'membrane',
    price: '5$'
}

let guitar = {
    type: 'electric',
    form: 'stratocaster',
    strings: 6,
    manufacturer: 'Fender'
}

//Object: change
keyboard.price = '10$'
guitar['form'] = 'telecaster'

// Comparison if
var age = +prompt("Сколько вам лет?","");
if (age < 18 && age > 0){
    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 && age <= 150 ) {
                    alert("как пенсия?");
                } else {
                    alert("то ли киборг, то ли ошибка");
                }
            }
        }
    }
}

//Comparison: sizes
let rusSize = prompt('Введите НАШ формат размера для переводa на на Американский формат','')
let usSize = [
    {
        number: [6],
        men: 'S',
        woman:['XXS']
    },
    {
        number: [8, 10],
        men: 'M',
        woman:['XXS', 'XS']
    },
    {
        number: [12, 14],
        men: 'L',
        woman:['S', 'M']
    },
    {
        number: [16, 18],
        men: 'XL',
        woman:['L','XL']
    },
    {
        number: [20, 22],
        men: 'XXL',
        woman:['XXL', 'XXXL']
    },
]

if(+rusSize && +rusSize >= 40 && +rusSize <= 56) {
    rusSize = +rusSize
    let flag = 0
    if(rusSize % 2 !== 0) {
        rusSize--
        flag++
    }
    for(let i = 0; i < usSize.length; i++) {
        let temp = usSize[i].number.findIndex(element => { return element === (rusSize - 34)})
        if(temp >= 0) {
            alert(`
                ${rusSize+flag}-й размер в американском формате:
                ------------------------------------------
                    Размер: ${usSize[i].number[temp]}
                    Мужчины: ${usSize[i].men}
                    Женщины: ${usSize[i].woman[temp]}
                ------------------------------------------
            `)
            break;
        }
    }
} else {
    alert('Неправильный размер.')
}

//Ternary
alert(`${confirm('Вы мужчина?')? 'Вы мужчина.':'Вы женщина.'}`)

//flats
function flats(floors, flatsPerFloor, flatNumber) {
    let info = {
        flat: flatNumber,
        building: Math.ceil(flatNumber/(floors * flatsPerFloor)),
    }
    info.floor = flatNumber/flatsPerFloor > floors? Math.ceil(flatNumber/flatsPerFloor)-floors*(info.building-1) : Math.ceil(flatNumber/flatsPerFloor);
    return info;
}
