I don't understand the basic JavaScript Syntax yet


#1


I feel like I've skipped something without knowing it somehow. I feel confident in python, and Java is similar, but I just don't understand how syntax works in terms of putting things "In" functions or "in"statements.

I had been writing if else like:
if(something){
Do this;
}
else{
Do this instead
}

But this exercise changed my code (the code it commented out) by putting the else in front of a curly bracket like:
}else

I need to see an example of if statements with if/else statements in them. I have no idea how the syntax is supposed to go and the lessons didn't really touch on white spaces or syntax very well. Does white space even matter in Java?


6. What if choice1 is rock?

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/2?curriculum_id=506324b3a7dffd00020bf661


var compare=function(choice1, choice2){
if(choice1 === choice2){
    return "The result is a tie!";
} else if(choice1 === "rock") {
    if(choice2 === "scissors") {
        return "rock wins";
} else {
        return "paper wins"
}
}


#2

if(a === b) {
   if(c === d) {
      //...
   }else if(e === f) {
      if(g === h) { 
         //...
      }else{
         //...
      }
   }else{
      //...
   }
}else if(i === j) {
   //...
}else{
   //...
}

In JavaScript, most people use }else{ instead of separating the } from else{. It's one line shorter and doesn't make the code harder to read. Some people use } else{ or } else {, it doesn't change how your code will run - it's just personal preference.


#3

Thank you, seeing it like that made me understand the whole curly bracket/ whitespace thing I was having trouble with.

I think I get it now.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.