These are chat archives for MontCode/GeneralChat

29th
Jan 2017
Matt Richards
@mattjrichards
Jan 29 2017 21:57
function testSize(num) {
  // Only change code below this line
 if (num < 5) {
   return "Tiny";
 }  else if (num < 10) {
   return "Small";
 }  else if (num < 15) {
   return "Medium";
 }  else if (num < 20) {
   return "Large";
 }  if (num >= 20) {
   return "Huge";
 }  else 

  return "Change Me";
  // Only change code above this line
}

// Change this value to test
testSize(7);
I have a question. Why, for the last else statement, does there not need to be an opening and closing curly brace after it? All the other statements have one, but for the last "else" statement it doesn't seem to be required. It is Lesson #189, Chaining If Else Statements.
Cody Loyd
@codyloyd
Jan 29 2017 22:05

it is possible to 'oneline' if statements and they don't require curly brackets:

if (sometingtrue) return something

this breaks down a bit with else-if statements because of how they're chained together.. but I assume that's what's happening here.. the JS engine doesn't care about whitespace in many cases so it's reading the last else as:

else return "Change Me";
Matt Richards
@mattjrichards
Jan 29 2017 22:18
I put curly braces around the last return statement just for the heck of it and it didn't seem to make a difference. Everything executed fine, so I guess it really doesn't make a difference?
Cody Loyd
@codyloyd
Jan 29 2017 22:21
Indeed
Brackets are better for the sake of clarity
But it works either way
Matt Richards
@mattjrichards
Jan 29 2017 22:22
@codyloyd Yes, I think I like the brackets for the sake of clarity.
@codyloyd thank you
MontBot
@MontBot
Jan 29 2017 22:23
mattjrichards sends cookie points to @codyloyd :sparkles: :thumbsup: :sparkles:
:cookie: 293 | @codyloyd |http://www.freecodecamp.com/codyloyd