2013-11-13 2 views
1

Это программа для оценки приложений модератора для форума сервера minecraft. Интересно, почему ничего не выводится, кроме значения по умолчанию. Помоги мне, пожалуйста.Почему ничего не происходит на консоли? {JavaScript}

код находится здесь:

 var moderatorApplicant=new Object(); 
moderatorApplicant.active=true; //boolean; 
moderatorApplicant.age=15; //number; 
moderatorApplicant.applicationLength="long"; //string, "short" or "long"; 
console.log("Moderator Application Judge Result:"); 
switch(moderatorApplicant) 
{ 
case (moderatorApplicant.active===true&&moderatorApplicant.age>=14&&moderatorApplicant.applicationLength=="long"): 
    console.log("You are fit for a mod! +1"); 
    break; 
case (moderatorApplicant.active===true&&moderatorApplicant.age<14&&moderatorApplicant.applicationLength=="long"): 
    console.log("You're active! You're app is long! You aren't 14 or over though, so +0."); 
    break; 
case (moderatorApplicant.active===true&&moderatorApplicant.age>=14&&moderatorApplicant.applicationLength=="short"): 
    console.log("App is short, but you're active, and you meet age requirements. +0"); 
    break; 
case (moderatorApplicant.active===true&&moderatorApplicant.age<14&&moderatorApplicant.applicationLength=="short"): 
    console.log("You're active, your app is short, and you are younger than 14. -1"); 
    break; 
case (moderatorApplicant.active===false&&moderatorApplicant.age>=14&&moderatorApplicant.applicationLength=="long"): 
    console.log("You are not active, but you are over 14 and have a long app. Try again when you are more active +0"); 
    break; 
case (moderatorApplicant.active===false&&moderatorApplicant.age<14&&moderatorApplicant.applicationLength=="long"): 
    console.log("You're young, you aren't active, but your app is long. -1"); 
    break; 
case (moderatorApplicant.active===false&&moderatorApplicant.age>=14&&moderatorApplicant.applicationLength=="short"): 
    console.log("You are not active. You made a short app, but you are over 14. -1"); 
    break; 
case (moderatorApplicant.active===false&&moderatorApplicant.age<14&&moderatorApplicant.applicationLength=="short"): 
    console.log("This is the definition of a bad application. Not active, younger than 14, and short app. -1."); 
    break; 
default: 
    console.log("Check again. Inappropriate values."); 
    break; 
} 

ответ

3

В JavaScript switch statements ожидают постоянное значение (буквальное число или строку). Вы должны использовать операторы if/else, чтобы иметь возможность использовать выражения для каждого случая.

1

Как Maurício Linhares уже ответил, инструкции switch не должны использоваться таким образом.

Здесь в качестве примера реорганизованной реализации, с использованием, если-заявления:

var moderatorApplicant= { 
    active   : true, //boolean; 
    age    : 15,  //number; 
    applicationLength : "long" //string, "short" or "long"; 
} 
console.log("Moderator Application Judge Result:"); 

var message; 

if (moderatorApplicant.active) { 
    if (moderatorApplicant.age >=14) { 
    if (moderatorApplicant.applicationLength=="long") { 
     message = "You are fit for a mod! +1"; 
    } else { 
     message = "App is short, but you're active, and you meet age requirements. +0"; 
    } 
    } else { 
    if (moderatorApplicant.applicationLength=="long") { 
     message = "You're active! You're app is long! You aren't 14 or over though, so +0."; 
    } else { 
     message = "You're active, your app is short, and you are younger than 14. -1"; 
    } 
    } 
} else { 
    if (moderatorApplicant.age >=14) { 
    if (moderatorApplicant.applicationLength=="long") { 
     message = "You are not active, but you are over 14 and have a long app. Try again when you are more active +0"; 
    } else { 
     message = "You are not active. You made a short app, but you are over 14. -1"; 
    } 
    } else { 
    if (moderatorApplicant.applicationLength=="long") { 
     message = "You're young, you aren't active, but your app is long. -1"; 
    } else { 
     message = "This is the definition of a bad application. Not active, younger than 14, and short app. -1."; 
    } 
    } 
} 

if (!message) message = "Check again. Inappropriate values."; 
console.log(message); 

Вот еще один пример, используя флаги вместо этого, и объекта со строками.

var moderatorApplicant= { 
    active   : true, //boolean; 
    age    : 15,  //number; 
    applicationLength : "long" //string, "short" or "long"; 
} 
console.log("Moderator Application Judge Result:"); 

var messages = { 
    'ayl' : "This is the definition of a bad application. Not active, younger than 14, and short app. -1.", 
    'ayL' : "You're young, you aren't active, but your app is long. -1", 
    'aYl' : "You are not active. You made a short app, but you are over 14. -1", 
    'aYL' : "You are not active, but you are over 14 and have a long app. Try again when you are more active +0", 
    'Ayl' : "You're active, your app is short, and you are younger than 14. -1", 
    'AyL' : "You're active! You're app is long! You aren't 14 or over though, so +0.", 
    'AYl' : "App is short, but you're active, and you meet age requirements. +0", 
    'AYL' : "You are fit for a mod! +1" 
}; 

var flags = 
(moderatorApplicant.active ? 'A' : 'a') + 
(moderatorApplicant.age >= 14 ? 'Y' : 'y') + 
(moderatorApplicant.applicationLength=='long' ? 'L' : 'l'); 

var message = messages[flags]; 
if (!message) message = "Check again. Inappropriate values."; 
console.log(message); 
Смежные вопросы