News:

If you need instructions on how to get through the hotels, check out the enclosed instruction book.

Main Menu

Programming Fast Ask

Farted by RomanClock, May 25, 2013, 02:57:46 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

RomanClock

If you have a relatively simple programming question, you can ask it hear and I, or another programmer, can try an assist you. Examples would be how to make preloaders, buttons, access properties of an object, math, or general programming concepts. I will also create an index with links to solutions that have been answered in this thread. If you have a more complex or multi-tiered problem, it may be better to create your own thread to allow discussion of specific cases.

Please check if your problem may have already been solved in this index:
(None Currently)
lemayo lol :soups:

I1I1I1I1I1I1I11111I1I1I1IIIIIII1I1I1I1I11I

I'd like to know what I should learn next after I learn javascript.
Quote from: PezDispenserclock;1948598Abba, I might not smoke weed, but I experiancing it being hit with a crowbar on a modded TTT server. Flashing colours, screen flipped, screen flying. Yup, I know how it\'s like.

RomanClock

Actionscript, Python, Java, C# (C Sharp) are pretty good places to start.
lemayo lol :soups:

I1I1I1I1I1I1I11111I1I1I1IIIIIII1I1I1I1I11I

Ok, so I need help understanding how this code works:

Quotevar array = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = 0;
for (var i = 0; i < array.length; i++){
  if (array > largest) {
    largest = array;
}
}
console.log(largest);

Basically, it finds the largest number in the "array" variable, but I don't quite know how it does it?
Quote from: PezDispenserclock;1948598Abba, I might not smoke weed, but I experiancing it being hit with a crowbar on a modded TTT server. Flashing colours, screen flipped, screen flying. Yup, I know how it\'s like.

RomanClock

Quote from: MightyBooshClock;1950888Basically, it finds the largest number in the "array" variable, but I don't quite know how it does it?

var array = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = 0;
for (var i = 0; i < array.length; i++){

This creates a for loop in the program using the variable i. The variable i starts at 0 (hence var i=0) and then a condition is defined. In this case, this loop will continue as long as i is < (less than) array.length (the number of elements in the array, in this case 8). The last bit, i++ is the iterator. This is equivalent to i += 1;. This step takes place at the "end" of the loop after all the code inside the loop is executed.
In short, it says:
Define the variable i with a value of 0 (var i=0;)
Loop while variable i is less than array.length (i)
Increase the value of i by one at the end of executing this loop (i++)

if (array[i] > largest) {
largest = array[i];
}
}

Here we simply compare the value of the variable largest with the element in array at index i.
Elements in an array can be accessed using a 0-based index. This means that if I have an array of [1, 2, 3], that array[0] is the first element in that array, which equals 1. A tricky thing in programming is that for a lot of data structures, the length may be 3, but the final element is located at the length -1.
Thus for array = [1, 2, 3], we have:
array.length is 3
array[0] is 1, array[1] is 2, and array[2] is 3
This is why in the for loop that i starts at 0, because the first element of the array is at 0. Then once it completes the execution of this (and the rest of the code in the for loop) it will increase the value of i. Therefore, we travel from element 0 to the last element in the array (at array.length-1);
Now in the if statement, we have array[i] > largest. This is comparing the value of array[i] with largest and if array[i] is larger, it changes the value of largest to equal that of array[i].
For example:
array = [3, 6, 2, 56, 32, 5, 89, 32]
largest = 0; array[0] = 3; Is 3>0? Yes, make largest = 3;
largest = 3; array[1] = 6; Is 6>3? Yes, make largest = 6;
largest = 6; array[2] = 2; is 2>6? No, exit this statement;

In total, the for loop makes a variable that is used to traverse the array and use an if statement to compare the values of the array. Hope that helps!
lemayo lol :soups:

I1I1I1I1I1I1I11111I1I1I1IIIIIII1I1I1I1I11I

Wow, that does help! Thank you :)
Quote from: PezDispenserclock;1948598Abba, I might not smoke weed, but I experiancing it being hit with a crowbar on a modded TTT server. Flashing colours, screen flipped, screen flying. Yup, I know how it\'s like.

I1I1I1I1I1I1I11111I1I1I1IIIIIII1I1I1I1I11I

Got another one for ya! So this is just a little choose your own adventure thingy
Quotevar user = prompt ("You are walking through the streets when you spy a trampoline with a large, Russian man beside it. Do you TALK to the Russian man, JUMP on the trampoline, or WALK away?").toUpperCase();

switch (user) {
    case 'TALK' :
        var speak = prompt ("You say a friendly hello to the mysterious Russian man. He starts walking toward you. Do you STAY or RUN away?").toUpperCase();
        switch (speak) {
            case 'STAY' {
                console.log ("You probably should have ran! As the Russian man draws nearer, you realize it is Zangief! You try to escape, but it is too late. His massive light SPD range grabs you, and he does a spinning piledriver, landing directly on your head. KO!")
                break;
}
            case 'RUN' {
                console.log ("You turn around to run, and as you do, you realize the man is Zangief! He promptly does an EX Banishing Flat, and you are KO'd.")
                break;
            }
        }
       
    case 'JUMP' :
        var hop = prompt ("You walk towards the trampoline. Do you DELAY your jump, or do you GO FOR IT?").toUpperCase();
        switch (hop) {
            case 'DELAY' {
                console.log ("You wait a moment before jumping. Suddenly you see the man leap into the air yelling I AM THE PROTECTOR OF RUSSIAS SKIES. You duck underneath him, do a dope flip off the trampoline, and continue on your way, unharmed.")
                break;
}
            case 'GO FOR IT' {
                console.log ("You leap onto the trampoline and bounce off. As you are in the air, the Russian man jumps up, revealing himself to be Zangief! He yells I AM THE PROTECTOR OR RUSSIAS SKIES, and grabs you. You are Siberean Blizzarded into the ground. KO!")
                break;
            }
}
    case 'WALK' :
        var walk = console.log ("You walk away unharmed. Probably smart of you.")
    break;
    default {
        console.log ("I don't quite understand your answer. Please select either TALK, JUMP or WALK.")
    }
}

When I try to run it it says "SyntaxError: Unexpected token {"
Quote from: PezDispenserclock;1948598Abba, I might not smoke weed, but I experiancing it being hit with a crowbar on a modded TTT server. Flashing colours, screen flipped, screen flying. Yup, I know how it\'s like.

RomanClock

#7
Quote from: MightyBooshClock;1952607Got another one for ya! So this is just a little choose your own adventure thingy
When I try to run it it says "SyntaxError: Unexpected token {"

Well that looks like its probably Javascript. It seems that you aren't clear on how to implement a switch/case structure. The proper way to use a switch statement is as follows:

switch (variable_to_compare) {
   case some_value_1:
      //execute code
      break;
   case some_value_2:
      //execute code
      break;
   default:
      //execute code
}
Each case starts with a value which you check against the variable you designated in the switch(). If that variable is equal to that value, it starts to execute all the code after it. When you want it to stop executing code, you add break; which will exit the switch statement. Optionally, you can use a default: case, if there are no matches to any of the case statements, it will execute the code in the default case block.

In your code, I see that after your case statements you put a { when it should be a :. You also shouldn't put switch statements inside switch statements. In general, you should also use an if/else structure for these kinds of things anyway as switch statements are messy and hard to maintain and for others to understand.

However if you're making a choose your own adventure you would probably want a structure like this:

var alive = true;
var path = gamePath0();
while(alive){
   if(path==1){
      path = gamePath1();
   } else if(path==2){
      //Display Game Over
      alive = false;
   }
}

function gamePath0()
   var path = 0;
   var x;
   while(path==0){
      x = prompt;
      if(x=='A'){
         //display effect
         path = 1;
      } else if(x=='B'){
         //display effect
         path = 2;
      }
   }
   return path;
}
lemayo lol :soups:

I1I1I1I1I1I1I11111I1I1I1IIIIIII1I1I1I1I11I

Ok, I'll try removing the inner switch statements and replacing them with if/else
Quote from: PezDispenserclock;1948598Abba, I might not smoke weed, but I experiancing it being hit with a crowbar on a modded TTT server. Flashing colours, screen flipped, screen flying. Yup, I know how it\'s like.

PhantomCatClock

I'd use a switch to handle their input, since it looks like this is a texty typey game instead of a clickular adventure (but I'm an idiot and don't listen to me) but you have a switch that already works if you'd just change those curly braces to colons


Actually, looking at that, I don't know why there's more than one switch... I guess it isn't textbased :v nevermind

I1I1I1I1I1I1I11111I1I1I1IIIIIII1I1I1I1I11I

Ok, so I made the curly brackets into colons, without changing the things into if/else statements for now. But now it just runs through each case no matter what I answer.

http://www.codecademy.com/courses/javascript-beginner-en-ZA2rb/0/4

There's the website that I'm using it on. And here's the updated code:
var user = prompt ("You are walking through the streets when you spy a trampoline with a large, Russian man beside it. Do you TALK to the Russian man, JUMP on the trampoline, or WALK away?").toUpperCase();

switch (user) {
    case 'TALK' :
        var speak = prompt ("You say a friendly hello to the mysterious Russian man. He starts walking toward you. Do you STAY or RUN away?").toUpperCase();
        switch (speak) {
            case 'STAY' :
                console.log ("You probably should have ran! As the Russian man draws nearer, you realize it is Zangief! You try to escape, but it is too late. His massive light SPD range grabs you, and he does a spinning piledriver, landing directly on your head. KO!");
                break;

            case 'RUN' :
                console.log ("You turn around to run, and as you do, you realize the man is Zangief! He promptly does an EX Banishing Flat, and you are KO'd.");
                break;
            }
   
       
    case 'JUMP' :
        var hop = prompt ("You walk towards the trampoline. Do you DELAY your jump, or do you GO FOR IT?").toUpperCase();
        switch (hop) {
            case 'DELAY' :
                console.log ("You wait a moment before jumping. Suddenly you see the man leap into the air yelling I AM THE PROTECTOR OF RUSSIAS SKIES. You duck underneath him, do a dope flip off the trampoline, and continue on your way, unharmed.");
                break;

            case 'GO FOR IT' :
                console.log ("You leap onto the trampoline and bounce off. As you are in the air, the Russian man jumps up, revealing himself to be Zangief! He yells I AM THE PROTECTOR OR RUSSIAS SKIES, and grabs you. You are Siberean Blizzarded into the ground. KO!");
                break;
            }

    case 'WALK' :
        var walk = console.log ("You walk away unharmed. Probably smart of you.");
    break;
    default :
        console.log ("I don't quite understand your answer. Please select either TALK, JUMP or WALK.");
   
}


As you can see, no matter what you reply with, it just keeps going to the next case in the code.  What would be causing that?
Quote from: PezDispenserclock;1948598Abba, I might not smoke weed, but I experiancing it being hit with a crowbar on a modded TTT server. Flashing colours, screen flipped, screen flying. Yup, I know how it\'s like.

PhantomCatClock

Oh yeah, that was the other weird thing about switch statements. You have to actually put BREAK at the end of them. Here's some lines from that awful game I made near the end of the Flash Flood:

case &quot;NORT&quot;:
case &quot;NOR&quot;:
case &quot;NO&quot;:
case &quot;N&quot;:
goDirection(&quot;n&quot;);
break;
case &quot;UP&quot;:
case &quot;U&quot;:
goDirection(&quot;u&quot;);
break;
case &quot;EAST&quot;:
case &quot;EA&quot;:
case &quot;E&quot;:
goDirection(&quot;e&quot;);
break;
case &quot;WEST&quot;:
case &quot;W&quot;:
case &quot;WE&quot;:
case &quot;WES&quot;:
goDirection(&quot;w&quot;);
break;
case &quot;SOUT&quot;:
case &quot;S&quot;:
case &quot;SOU&quot;:
case &quot;SO&quot;:
goDirection(&quot;s&quot;);
break;
case &quot;DOWN&quot;:
case &quot;DOW&quot;:
case &quot;D&quot;:
goDirection(&quot;d&quot;);
break;
case &quot;IN&quot;:
case &quot;I&quot;:
goDirection(&quot;i&quot;);
break;
case &quot;OUT&quot;:
case &quot;OU&quot;:
case &quot;O&quot;:
goDirection(&quot;o&quot;);
break;
case &quot;GO&quot;:
case &quot;G&quot;:
switch(noun){
case &quot;NORT&quot;:
case &quot;NOR&quot;:
case &quot;NO&quot;:
case &quot;N&quot;:
goDirection(&quot;n&quot;);
break;
case &quot;UP&quot;:
case &quot;U&quot;:
goDirection(&quot;u&quot;);
break;
case &quot;EAST&quot;:
case &quot;EA&quot;:
case &quot;E&quot;:
goDirection(&quot;e&quot;);
break;
case &quot;WEST&quot;:
case &quot;W&quot;:
case &quot;WE&quot;:
case &quot;WES&quot;:
goDirection(&quot;w&quot;);
break;
case &quot;SOUT&quot;:
case &quot;S&quot;:
case &quot;SOU&quot;:
case &quot;SO&quot;:
goDirection(&quot;s&quot;);
break;
case &quot;DOWN&quot;:
case &quot;DOW&quot;:
case &quot;D&quot;:
goDirection(&quot;d&quot;);
break;
case &quot;IN&quot;:
case &quot;I&quot;:
goDirection(&quot;i&quot;);
break;
case &quot;OUT&quot;:
case &quot;OU&quot;:
case &quot;O&quot;:
goDirection(&quot;o&quot;);
break;
default:
displayRoomDesc = &quot;Ã,©Well, where do you want to go?&quot;;
createDisplayText();
}
break;


It just goes on and on until it sees a break so you can do things like that

I1I1I1I1I1I1I11111I1I1I1IIIIIII1I1I1I1I11I

Quote from: PhantomCatClock;1952743Oh yeah, that was the other weird thing about switch statements. You have to actually put BREAK at the end of them. Here's some lines from that awful game I made near the end of the Flash Flood:


case &quot;NORT&quot;:
case &quot;NOR&quot;:
case &quot;NO&quot;:
case &quot;N&quot;:
goDirection(&quot;n&quot;);
break;
case &quot;UP&quot;:
case &quot;U&quot;:
goDirection(&quot;u&quot;);
break;
case &quot;EAST&quot;:
case &quot;EA&quot;:
case &quot;E&quot;:
goDirection(&quot;e&quot;);
break;
case &quot;WEST&quot;:
case &quot;W&quot;:
case &quot;WE&quot;:
case &quot;WES&quot;:
goDirection(&quot;w&quot;);
break;
case &quot;SOUT&quot;:
case &quot;S&quot;:
case &quot;SOU&quot;:
case &quot;SO&quot;:
goDirection(&quot;s&quot;);
break;
case &quot;DOWN&quot;:
case &quot;DOW&quot;:
case &quot;D&quot;:
goDirection(&quot;d&quot;);
break;
case &quot;IN&quot;:
case &quot;I&quot;:
goDirection(&quot;i&quot;);
break;
case &quot;OUT&quot;:
case &quot;OU&quot;:
case &quot;O&quot;:
goDirection(&quot;o&quot;);
break;
case &quot;GO&quot;:
case &quot;G&quot;:
switch(noun){
case &quot;NORT&quot;:
case &quot;NOR&quot;:
case &quot;NO&quot;:
case &quot;N&quot;:
goDirection(&quot;n&quot;);
break;
case &quot;UP&quot;:
case &quot;U&quot;:
goDirection(&quot;u&quot;);
break;
case &quot;EAST&quot;:
case &quot;EA&quot;:
case &quot;E&quot;:
goDirection(&quot;e&quot;);
break;
case &quot;WEST&quot;:
case &quot;W&quot;:
case &quot;WE&quot;:
case &quot;WES&quot;:
goDirection(&quot;w&quot;);
break;
case &quot;SOUT&quot;:
case &quot;S&quot;:
case &quot;SOU&quot;:
case &quot;SO&quot;:
goDirection(&quot;s&quot;);
break;
case &quot;DOWN&quot;:
case &quot;DOW&quot;:
case &quot;D&quot;:
goDirection(&quot;d&quot;);
break;
case &quot;IN&quot;:
case &quot;I&quot;:
goDirection(&quot;i&quot;);
break;
case &quot;OUT&quot;:
case &quot;OU&quot;:
case &quot;O&quot;:
goDirection(&quot;o&quot;);
break;
default:
displayRoomDesc = &quot;Ã,©Well, where do you want to go?&quot;;
createDisplayText();
}
break;



It just goes on and on until it sees a break so you can do things like that

Ah, that fixed it. Thank you based god.
Quote from: PezDispenserclock;1948598Abba, I might not smoke weed, but I experiancing it being hit with a crowbar on a modded TTT server. Flashing colours, screen flipped, screen flying. Yup, I know how it\'s like.

RomanClock

#13
Quote from: MightyBooshClock;1952741Ok, so I made the curly brackets into colons, without changing the things into if/else statements for now. But now it just runs through each case no matter what I answer.

http://www.codecademy.com/courses/javascript-beginner-en-ZA2rb/0/4

There's the website that I'm using it on. And here's the updated code:

As you can see, no matter what you reply with, it just keeps going to the next case in the code.  What would be causing that?

That is because you are not using the keyword break; to exit the switch statement when youre done using it.

Also I implore you to not use nested switches ever and that you make an effort to understand and implement the following code which I previously constructed. It will allow for much easier alterations to your story, the addition of new paths, encourages good coding practices, and is much easier to maintain and understand overall.

var alive = true;
var path = gamePath0();
while(alive){
   if(path==1){
      path = gamePath1();
   } else if(path==2){
      //Display Game Over
      alive = false;
   }
}

function gamePath0()
   var path = 0;
   var x;
   while(path==0){
      x = prompt;
      if(x=='A'){
         //display effect
         path = 1;
      } else if(x=='B'){
         //display effect
         path = 2;
      }
   }
   return path;
}
lemayo lol :soups:

I1I1I1I1I1I1I11111I1I1I1IIIIIII1I1I1I1I11I

Alright :) Would you mind describing how that code works to me?
Quote from: PezDispenserclock;1948598Abba, I might not smoke weed, but I experiancing it being hit with a crowbar on a modded TTT server. Flashing colours, screen flipped, screen flying. Yup, I know how it\'s like.

RomanClock

Quote from: MightyBooshClock;1952771Alright :) Would you mind describing how that code works to me?

var alive = true;The "Game Over" variable. While it is true, you will keep playing the game. Otherwise the game is over.
var path = gamePath0();Path is the variable that holds where you are in the adventure. You can use numbers or names/phrases.
while(alive){Create a loop that continues until you game over.
  if(path==1){
      path = gamePath1();
   } else if(path==2){
      //Display Game Over
      alive = false;
   }
}
An if/else structure used to check where you are in the game. Based on the value of path, it will execute the function according to where you are.
function gamePath0(){Declares a new function. Each time you have a set of choices, you would make a function. Each function contains a prompt and returns a new value for path used to figure out where you are in the game.
  var path = 0;Declares path within the function. As long as it is 0, it will keep displaying the prompt.
  var x;Just declares x exists, it will be used later.
  while(path==0){A while loop that will continue to display the prompt until you give it a valid reply.
     x = prompt;The prompt!
     if(x=='A'){
         //display effect
         path = 1;
      } else if(x=='B'){
         //display effect
         path = 2;
      }
   }
Compares x to a set of valid answers. If it matches a value, it will set path to the appropriate value and display a message. If no valid answer is given, it will just ask for a new answer.
  return path;
}
Returns the value of path. This sends it back to the primary game loop (that first while loop) where the new value will be tested and a new function will be executed, thus continuing your game.

To add a path in the primary while loop (the first loop at the top) you would do something like this:
//previous code
} else if(path=='VALUE'){
   path = gamePathXXX();
} ...
Then you would create the function to go with it:
function gamePathXXX()
   var path = 0;
   while(path==0){
      x = prompt;
      if(x=='YES'){
         //display effect
         path = 'GoodResult';
      } else if(x=='NO'){
         //display effect
         path = 'BadResult';
      }
   }
   return path;
}
lemayo lol :soups: