If statements and functions

Does anybody have some advice of organizing multiple if statements in a function? Something we should know as beginners?
Thank you.

1 Like

If you have a lengthy if/else block then a switch statement might be a viable alternative.

You might also break parts of the logic into different functions. You might also be able to use an object/dictionary approach. Do you have an example?

Sounds like something i will study further in freecodecamp so i’ll let it come to me later in the course. I don’t want to jump into conclusion of how the switch works now and get mixed up as the definition above looks a little long to understand.

1 Like

If I remember correctly, I think is just one lesson on switch statements.

If you are at the very beginning of the javascript lessons then it is probably coming up.

2 Likes

I noticed when studying the greater than and less than operators, that if statements in multiple if statement function, with higher number comparison are written first when the greater than > operator is used then lower comparisons come next. However when the less than < operator is used the if statements with lower numbers i want to compare the argument to are first then the lower comparison come next until the minimum number where i write this line: return " numberX or under";
So what is i want to used both operators, how to avoid misunderstanding my code or for it nor to work.

I’m not sure what you are saying.

When using chained if/else, there is no rule that the greater than operator has to be first. It really just depends on what you are trying to do.

It sounds like you are getting a little ahead of yourself. I like that you’re already thinking about keeping the code clean, but as you learn you will get more tools to help you do that.

1 Like

Here is an exemple :

Function test(val) {
If (val > 100){
      Return 'greater than 100';
}
If (val > 50) {
     Return 'greater than 50';
}
Return ' 50 or less';
}
Test(101);

So this exemple above if i switch the first if statment in line with the second one, the function call will return ’ greater then 50’ but i won’t know if it is greater then 100.( If i didn’t know the argument is 101).
This something i observed and then i worried after i finish the javascript course if write some code in the future what advice should i need? What should i know in advance.

Same problem should occur in this exemple of less than operator.

Function test2(val) {
If (val < 50){
      Return 'greater than 100';
}
If (val < 100) {
     Return 'greater than 50';
}
Return ' 100 or more';
}

Right, but the inverse of > is not < but is <=. Here is an example:

function test1(val) {
  if (val > 100){
    return 'greater than 100';
  }
  if (val > 50) {
    return 'greater than 50';
  }
  return '50 or less';
}

function test2(val) {
  if (val <= 50){
    return '50 or less';
  }
  if (val <= 100) {
    return 'greater than 50';
  }
  return 'greater than 100';
}

for (let i = 0; i <= 110; i++) {
  if (test1(i) === test2(i)) {
    console.log('For', i, ', they match!')
  } else {
    console.log('For', i, ', they don\'t match!!! What went wrong?')
  }
}

In this, func1 is what you had before, and func2 is the reversed logic. The for loop just confirms that they both work the same.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

this is useful.
why do i think that the for loop will find that they will match only if the second if statement in each function is executed ( since both would return 'greater than 50) otherwise I don’t understand the for loops yet since I’m not in the loops level of freecodecamp course? but opened my eye to something new too.

OK, then don’t worry about the for loop. All it is doing is testing if the two functions return the same value for every number from 0 to 110.

1 Like

basically, I do not have any problem understanding the if statements, it is just that I observed that the code is executed in order from top to down and thought if i did this :

function test2(val) { 
   if (val <=100) {
      return 'greater than 50';
  }
  if (val <= 50){ /*here if the val is greater than or equal to 100, this won't be executed, and I won't know if it is less than or equal to 50.  so this if statement should be first.*/
    return '50 or less';
  }
 
  return 'greater than 100';
}

it will return ‘greater than 100’ and the second if statement won’t be executed and i won’t know if the argument number i gave to the function is greater or equal to 50 as well, you see what i mean? it is better when the if statements are organized. just what i am saying.

It’s just the logic of it. Visually work through your function. What if it is passed the values 100, 75, 50, or 25? I think specifically the last one should be of interest.

1 Like

but thanx for reacting to different aspects of my observation and examples, you introduced to the for loop very well, it looked like a math problem ive seen back in school.
this forum is fun.

1 Like

When it comes to planning out your conditional logic (if/elses), drawing out a flowchart is really really helpful.

3 Likes

just one last thing, may I know how did you know that the inverse of > is <= and not < ?
is it something they teach in coding schools? or you learned it by coincidence?

I don’t know, it’s just sort of a logic thing. I mean, you have to account for that = somehow. I’m sure there is some number theory reason and some mathematical proof. I don’t know, maybe I learned it at some point. Or maybe I just learned it through experience.

It’s easy to test - just pick values for x and y and compare <, >, <=, and >=.

2 Likes

If I tell you to write down every number that’s greater than 5, you have 6, 7, 8, 9,…

The ‘opposite’ of that list would be 5, 4, 3, 2, 1,…

Those are numbers less than or equal to 5.

Nothing magic or special logic. It’s looking at what > means.

3 Likes

There are little “tricks” like this. Some of us learned them in school. Some of us learned them by trial and error. You don’t have to be a mathematical genius to be a good programmer (for most coding jobs), but it is helpful to develop low level, practical knowledge like this, little tricks and understandings. But don’t be alarmed if they aren’t all immediately obvious - you will collect them over time.

2 Likes