function truncateFraction (number) { try { let strNum = String(number).split('.') if(strNum[1].length > 2) { strNum[1] = strNum[1].slice(0, 2) } return Number(strNum.join('.')) } catch (e) { return number } } var loop = true while(loop) { switch(prompt('input task name:', '')) { case 'switch: sizes': 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'] }, ] let rusSize = prompt('Введите НАШ формат размера для переводa на на Американский формат','') console.log(isNaN(rusSize)) switch(!isNaN(rusSize) && +rusSize >= 40 && +rusSize <= 56) { case true: rusSize = +rusSize let flag = 0 switch(rusSize % 2 !== 0) { case true: rusSize-- flag++ } for(let i = 0; i < usSize.length; i++) { let temp = usSize[i].number.findIndex(element => { return element === (rusSize - 34)}) switch( temp >= 0) { case true: alert(` ${rusSize+flag}-й размер в американском формате: +---------------------------------------+ Размер: ${usSize[i].number[temp]} Мужчины: ${usSize[i].men} Женщины: ${usSize[i].woman[temp]} +---------------------------------------+ `) i = usSize.length break; default: continue; } } break; case false: alert('Неправильный размер.') break; } break; case 'switch: if': let color = prompt("Введите цвет",""); if(color === 'red') { document.write("
красный
"); document.write("
черный
"); } else if(color === 'blue') { document.write("
синий
"); document.write("
зеленый
"); } else if(color === 'green') { document.write("
зеленый
"); } else if(color === 'black') { document.write("
черный
"); } else { document.write("
Я не понял
"); } alert('aborting execution to make changes. Reload page to return to select mode') loop = false break; case 'prompt: or': let age = prompt('Введите свой возраст', '') || alert('Вы не ввели ничего!') if (age && isNaN(age) || age > 200 || age < 0){ alert('Вы ввели неверные данные!') } else if(age){ age = age = Math.trunc(Number(age)) res = new Date().getFullYear() - age alert(`Исходя из введенного вами возраста вы родились в ${res}г. либо ${res-1}г.`) } break; case 'confirm: or this days': confirm('шоппинг') || alert('ты-бяка') break; case 'confirm: if this days': if(!confirm('шоппинг')) { alert('ты-бяка') } break; case 'triple prompt': let Name = prompt('your Name:','') let surname = prompt('your Surname:','') let patronymic = prompt('your Patronymic:','') alert(`${surname} ${Name} ${patronymic}`) break; case 'default: or': let defaultName = prompt('your Name:','') || 'Иван' let defaultSurname = prompt('your Surname:','') || 'Иванов' let defaultPatronymic = prompt('your Patronymic:','') || 'Иванович' alert(`${defaultSurname} ${defaultName} ${defaultPatronymic}`) break; case 'default: if': let NameIf = prompt('your Name:','') let surnameIf = prompt('your Surname:','') let patronymicIf = prompt('your Patronymic:','') if(NameIf) { NameIf = NameIf } else { NameIf = 'Ivan' } if(surnameIf) { surnameIf = surnameIf } else { surnameIf = 'Ivanov' } if (patronymicIf) { patronymicIf = patronymicIf } else { patronymicIf = 'Ivanovich' } alert(`"if else" to add default values: ${surnameIf} ${NameIf} ${patronymicIf}`) break; case 'login and password': if(prompt('login','') === 'admin') { alert('correct login') if(prompt('password','') === 'qwerty') { alert('correct password') } else { alert('wrong password') } } else { alert('wrong login') } break; case 'currency calc': let curr = prompt('type currency: "eur" or "usd"','') let transferLog = { initial: 'hrn', to: '', ratios: { sold: { usd: 0.032, eur: 0.037, setUsd: function(value) { this.usd = value }, setEur: function(value) { this.eur = value } }, bought: { usd: 0.033, eur: 0.038 } } } if(curr) { // switch(curr.toLowerCase()) { // case 'eur': // transferLog.to = 'eur' // confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['usd'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['usd'], 'bought'] // break; // case 'usd': // transferLog.to = 'usd' // confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['eur'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['eur'], 'bought'] // break; // } if(curr.toLowerCase() === 'eur') { transferLog.to = 'eur' confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['usd'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['usd'], 'bought'] } if(curr.toLowerCase() === 'usd') { transferLog.to = 'usd' confirm('Sell press "Ok" | Buy press "Cancel"')? transferLog.curr = [transferLog.ratios['sold']['eur'], 'sold'] : transferLog.curr = [transferLog.ratios['bought']['eur'], 'bought'] } if(transferLog.to === 'eur' || transferLog.to === 'usd') { transferLog.money = prompt('input amount of money to give', '') if(!+transferLog.money) { alert('invalid input') } else { transferLog.given = Number(transferLog.money) if(transferLog.curr[1] === 'sold') { transferLog.money = truncateFraction(transferLog.given / transferLog.curr[0]) transferLog.message = `You've ${transferLog.curr[1]} ${transferLog.given}${transferLog.to} for ${transferLog.money}${transferLog.initial}` } else if(transferLog.curr[1] === 'bought') { transferLog.money = truncateFraction(transferLog.given * transferLog.curr[0]) transferLog.message = `You've ${transferLog.curr[1]} ${transferLog.money}${transferLog.to} for ${transferLog.given}${transferLog.initial}` } transferLog.money > 0? alert(transferLog.message) : alert(`Looks like there is too little amount of hrn given to translate to ${transferLog.to}`) } } else { alert('invalid input') } } else { alert('No input.') } break; case 'scissors': let combos = { paper: { conter: 'scissors' }, scissors: { conter: 'rock' }, rock: { conter: 'paper' } } let user = prompt('rock, paper, scissors?').toLowerCase() if(user === 'rock' || user === 'paper' || user === 'scissors') { let computer = Math.random() if(computer >= 0.6) { computer = 'scissors' } else if(computer <= 0.3) { computer = 'paper' } else { computer = 'rock' } alert(`computer: ${computer}`) if(user === computer) { alert('TIE') } else { combos[computer].conter === user ? alert('USER WINS.') : alert('COMPUTER WINS.') } } else { alert('user disqualified! Compuer wins!') } break; case 'real data': let currReal = prompt('type currency: "eur" or "usd"','') if(currReal && currReal.toLowerCase() === 'eur' || currReal.toLowerCase() === 'usd') { currReal = currReal.toLowerCase() let sellReal = confirm('Sell press "Ok" | Buy press "Cancel"') let operationReal = sellReal? 'sold' : 'bought' let givenReal = prompt('input amount of money you give:','') if(givenReal && !isNaN(givenReal)) { givenReal = truncateFraction(Number(givenReal)) fetch(`https://open.er-api.com/v6/latest/${currReal.toUpperCase()}`).then(res => res.json()) .then(data => { let got = sellReal ? truncateFraction(givenReal * data.rates.UAH) : truncateFraction(givenReal / data.rates.UAH) if(got > 0) { sellReal ? alert(`You've ${operationReal} ${givenReal}${currReal} for ${got}hrn`): alert(`You've ${operationReal} ${got}${currReal} for ${givenReal}hrn`) } else { alert(`Looks like there is too little amount of money been given to make such operation`) } }) } else { alert('invalid input') } } else { alert('invalid input') } break; case 'black belt': let combos2 = { paper: { conter: 'scissors' }, scissors: { conter: 'rock' }, rock: { conter: 'paper' } } let user2 = prompt('rock, paper, scissors?').toLowerCase() let computer2 = Math.random() >= 0.6 ? 'scissors' : (Math.random() <= 0.3 ? 'paper' : 'rock') alert(`computer: ${computer2}`) user2 === 'rock' || user2 === 'paper' || user2 === 'scissors'? (computer2 === user2? alert('TIE') : combos2[computer2].conter === user2 ? alert('USER WINS.') : alert('COMPUTER WINS.')) : alert('user disqualified! Compuer wins!') break; case null: loop = false break; default: alert('no matches found:(') } }