простая задачка JS

202
16 октября 2017, 00:35

Прога должна выявить сколько дней осталось до нового года! В чем заключается моя ошибка?

Выводит: 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'); 
}

Answer 1

В 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'); 
 
} 
  

Answer 2

Вводимые юзером значения в 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'); 
 
}

READ ALSO
как сделать запрос на сторонний домен, используя node js?

как сделать запрос на сторонний домен, используя node js?

задачи состоит в том, чтобы сделать ajax запрос на сторонний доменвозможно это сделать используя модуль request? можно увидеть конкретный пример,...

221
Регулярное выражения добавит строку

Регулярное выражения добавит строку

Помогите добавить строку, например есть атрибут:

249