Прога должна выявить сколько дней осталось до нового года! В чем заключается моя ошибка?
Выводит: your inputs is not correct
var real;
var answer;
var days;
var year = 365;
var yMonth = prompt('Enter your month(with numbers)');
var yDay = prompt('Enter your day(with numbers)');
switch (yMonth) {
case 1:
{
real = 31;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 2:
{
real = 59;
days = 28;
answer = (year - real + (days - yDay));
console.log(answer);
break;
}
case 3:
{
real = 90;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 4:
{
real = 120;
days = 30;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 5:
{
real = 151;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 6:
{
real = 181;
days = 30;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 7:
{
real = 212;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 8:
{
real = 243;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 9:
{
real = 273;
days = 30;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 10:
{
real = 304;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 11:
{
real = 334;
days = 30;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 12:
{
real = 365;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
default:
alert('your inputs is not correct');
}
В yMonth и yDay после ввода пользователя будут строки. В switch вы сравниваете их с числами, поэтому совпадений нет. Самый простой вариант преобразовать ввод пользователя к числу:
var yMonth=+prompt('Enter your month(with numbers)');
var yDay=+prompt('Enter your day(with numbers)');
var real;
var answer;
var days;
var year=365;
var yMonth=+prompt('Enter your month(with numbers)');
var yDay=+prompt('Enter your day(with numbers)');
switch (yMonth) {
case 1:{real=31;
days=31;
answer=year-real+(days-yDay);
console.log(answer);
break;
}
case 2:{real=59;
days=28;
answer=(year-real+(days-yDay));
console.log(answer);
break;}
case 3:{real=90;
days=31;
answer=year-real+(days-yDay);
console.log(answer);
break;}
case 4:{real=120;
days=30;
answer=year-real+(days-yDay);
console.log(answer);
break;}
case 5:{real=151;
days=31;
answer=year-real+(days-yDay);
console.log(answer);
break;}
case 6:{real=181;
days=30;
answer=year-real+(days-yDay);
console.log(answer);
break;}
case 7:{real=212;
days=31;
answer=year-real+(days-yDay);
console.log(answer);
break;}
case 8:{real=243;
days=31;
answer=year-real+(days-yDay);
console.log(answer);
break;}
case 9:{real=273;
days=30;
answer=year-real+(days-yDay);
console.log(answer);
break;}
case 10:{real=304;
days=31;
answer=year-real+(days-yDay);
console.log(answer);
break;}
case 11:{real=334;
days=30;
answer=year-real+(days-yDay);
console.log(answer);
break;}
case 12:{real=365;
days=31;
answer=year-real+(days-yDay);
console.log(answer);
break;}
default:alert('your inputs is not correct');
}
Вводимые юзером значения в prompt
являются строками. Чтобы привести их к числовому типу, можно использовать, например, parseInt()
:
var real;
var answer;
var days;
var year = 365;
var yMonth = parseInt(prompt('Enter your month(with numbers)'));
var yDay = parseInt(prompt('Enter your day(with numbers)'));
switch (yMonth) {
case 1:
{
real = 31;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 2:
{
real = 59;
days = 28;
answer = (year - real + (days - yDay));
console.log(answer);
break;
}
case 3:
{
real = 90;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 4:
{
real = 120;
days = 30;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 5:
{
real = 151;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 6:
{
real = 181;
days = 30;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 7:
{
real = 212;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 8:
{
real = 243;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 9:
{
real = 273;
days = 30;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 10:
{
real = 304;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 11:
{
real = 334;
days = 30;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
case 12:
{
real = 365;
days = 31;
answer = year - real + (days - yDay);
console.log(answer);
break;
}
default:
alert('your inputs is not correct');
}
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
задачи состоит в том, чтобы сделать ajax запрос на сторонний доменвозможно это сделать используя модуль request? можно увидеть конкретный пример,...
Код на скринеТестил на фри хостинге 000webhost - все ок