Javascript Game Help?

General technological topics without their own forum go here

Javascript Game Help?

Post by -Ninjex- on Tue Jan 01, 2013 6:37 pm
([msg=71945]see Javascript Game Help?[/msg])

I was trying to give some support at Code Cademy by creating a new game which users would have to program to get their JavaScript techniques up. Here is what I have so far:

Code: Select all
//Check if user is ready to play!
confirm("Are you ready to play?");

//Checking age for increased difficulty
var age = prompt("What is your age?");

//Selecting Difficulty for even more increased difficulty
var difficulty = prompt("Type your difficulty: easy, medium, or hard");

//Creating a confirm difficulty box

confirm("Please confirm that you want to play on " + difficulty + "?");

//Here we will create Variables of all sorts (questions, answers, and winning / losing output) to help us with our statements

/*Create math problems for difficulties
People under 18 are given questions easy, medium, and hard
People over 18 are given questions easy2, medium2, and hard2
*/
var easy = "What is 5+5?";
var easy2 = "What is 15+15?";
var medium = "What is 8*6?";
var medium2 = "What is 22*19?";
var hard = "What is 105 % 18?";
var hard2 =" What is 1893 % 12?";

//Creating answers for math problems
var easy_A = "10";
var easy2_A = "30";
var medium_A = "48";
var medium2_A = "418";
var hard_A = "15";
var hard2_A = "9";

/*Creating variables for winning and losing output,
since it will be used multiple times
*/
var winner = "Congratulations, not bad for a " + age + " year old, you won!";
var loser = "I am sorry, you failed... Maybe " + difficulty + " is too hard for you!";

//Creating if else statement for easy
if (age < 18 && difficulty === "easy")
//Creating Variable to store user's answer for easy
var easyA = prompt(easy);
//Did the player win
if (easyA === easy_A)
{
   console.log(winner);

}
else
{
   console.log(loser);
}

//Creating if else statement for easy2
if (age >= 18 && difficulty === "easy")
//Creating variable to store user's answer for easy2
var easy2A = prompt(easy2);
//Did the player win
if (easy2A === easy2_A)
{
   console.log(winner);
}
else
{   
   console.log(loser);
}

//Creating if else statement for medium
if (age < 18 && difficulty === "medium")
//Creating variable to store user's answer for medium
var mediumA = prompt(medium);
//Did the player win
if (mediumA === medium_A)
{
   console.log(winner);
}
else
{   
   console.log(loser);
}

//Creating if else statement for medium2
if (age >= 18 && difficulty === "medium")
//Creating variable to store user's answer for medium2
var medium2A = prompt(medium2);
//Did the player win
if (medium2A === medium2_A)
{
   console.log(winner);
}
else
{   
   console.log(loser);
}

//Creating if else statement for hard
if (age < 18 && difficulty === "hard")
//Creating variable to store user's answer for hard
var hardA = prompt(hard);
//Did the player win
if (hardA === hard_A)
{
   console.log(winner);
}
else
{   
   console.log(loser);
}

//Creating if else statement for hard2
if (age >= 18 && difficulty === "hard")
//Creating variable to store user's answer for hard2
var hard2A = prompt(hard2);
//Did the player win
if (hard2A === hard2_A)
{
   console.log(winner);
}
else
{   
   console.log(loser);
}


Basically, I can only make the game based on what we have already overviewed:

Data Types: numbers, strings, booleans
Variables
If else Statements
console.log
substring
.length
Math
prompt
confirm
comments

The issue is that it executes all five of the "Did the player win" if else statements

Anyone know a quick fix?
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1355
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Javascript Game Help?

Post by fashizzlepop on Tue Jan 01, 2013 9:07 pm
([msg=71946]see Re: Javascript Game Help?[/msg])

Code: Select all
if (difficulty === 'easy')
{
    if (age < 18)
    {
        *this*
    }
    else
    {
        *that*
    {
}
else if (difficulty === 'medium')
{
    if age < 18
    {
...
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Javascript Game Help?

Post by -Ninjex- on Tue Jan 01, 2013 9:42 pm
([msg=71948]see Re: Javascript Game Help?[/msg])

fashizzlepop wrote:
Code: Select all
if (difficulty === 'easy')
{
    if (age < 18)
    {
        *this*
    }
    else
    {
        *that*
    {
}
else if (difficulty === 'medium')
{
    if age < 18
    {
...


Thanks for the response!
I will re-code it as so, and hope they take it.
I am not sure, as this would be a if else if statment not a if else
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1355
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Javascript Game Help?

Post by fashizzlepop on Wed Jan 02, 2013 12:51 am
([msg=71949]see Re: Javascript Game Help?[/msg])

With only if else as a construct, there is no way to NOT go through each if statement. And there's no way to know if it missed every single one.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Javascript Game Help?

Post by -Ninjex- on Wed Jan 02, 2013 1:14 am
([msg=71950]see Re: Javascript Game Help?[/msg])

fashizzlepop wrote:With only if else as a construct, there is no way to NOT go through each if statement. And there's no way to know if it missed every single one.


Okay, I didn't think there was, that's why I was asking.
Thanks for the help, I might build this for their second chapter game!
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1355
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)



Return to General

Who is online

Users browsing this forum: No registered users and 0 guests