JSFiddle - React, Tailwind, and code Playground
by Mariya_Korotkova
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
JavaScript
let money, time;
function start() {
money = +prompt('Ваш бюджет на месяц?', '');
time = prompt('Введите дату в формате YYYY-MM-DD', '');
/* isNaN возвращает true, когда туда попадают не цифры (т.е. пользователь ввел случайно символы, например)
=="" - чтобы пользователь не оставил пустую строку
==null , чтобы пользователь не нажал ОТМЕНА*/
while (isNaN(money) || money == '' || money == null) {
money = +prompt('Ваш бюджет на месяц?', '');
}
}
start();
let appData = {
budget: money,
timeData: time,
expenses: {},
optionalExpenses: {},
income: [],
savings: true
};
function chooseExpenses() {
for (let i = 0; i < 2; i++) {
let a = prompt('Введите обязательную статью расходов в этом месяце', ''),
b = prompt('Во сколько обойдется?', '');
if ((typeof (a)) === 'string' && (typeof (a)) != null && (typeof (b)) != null &&
a != '' && b != '' && a.length < 50) {
console.log('done');
appData.expenses[a] = b;
} else {
i--;
}
}
}
chooseExpenses();
// console.log(appData);
function detectDayBudget() {
appData.moneyPerDay = (appData.budget / 30).toFixed();
alert('Ежедневный бюджет: ' + appData.moneyPerDay + ' рублей');
}
detectDayBudget();
function detectLevel() {
if (appData.moneyPerDay < 100) {
console.log('Минимальный уровень достатка');
} else if (appData.moneyPerDay > 100 && appData.moneyPerDay < 2000) {
console.log('Средний уровень достатка');
} else if (appData.moneyPerDay > 2000) {
console.log('Высокий уровень достатка');
} else {
console.log('Произошла ошибка');
}
}
detectLevel();
function checkSavings() {
if (appData.savings == true) {
let save = +prompt('Какова сумма накоплений?'),
percent = +prompt('Под какой процент?');
appData.monthIncome = save / 100 / 12 * percent;
alert('Доход в месяц с вашего...