Solved

help please with conditionals else if statement!!!!!

Posted on 2016-10-22
4
60 Views
Last Modified: 2016-10-23
I have problem need help with below is the problem:

Objectives
After this exercise, you should be able to:

Demonstrate how to write an else if statement that checks many conditions
Exercise
Write a function named checkLock. This function should:

have four arguments that are all numbers.
return "correct" if the four numbers are a valid combination.
return "incorrect" if the four numbers aren’t a valid combination.
A combination is valid if:

the first number is a 3, 5, or 7.
the second number is 2.
the third number is between 5 and 100. 5 and 100 are both valid numbers.
the fourth number is less than 9 or greater than 20. 9 and 20 are both invalid numbers.
0
Comment
Question by:OMIED BADR
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
Michael Vasilevsky earned 500 total points
ID: 41855651
var checkLock = function(num1, num2, num3, num4) {
  if (num1 !== 3 && num1 !== 5 && num1 !== 7) {
    return "incorrect"
  } else {
    if (num2 !== 2) {
       return "incorrect"
    } else {
      if (num3 < 5 || num3 > 100) {
        return "incorrect"
      } else {
        if (num4 >= 9 && num4 <= 20) {
          return "incorrect"
        } else {   
          return "correct"
        }
      }
    }
  }
}

checkLock(7,2,100,-1);

Open in new window

0
 

Author Comment

by:OMIED BADR
ID: 41855680
thank you so much you can close the question it worked.
0
 
LVL 10

Expert Comment

by:Michael Vasilevsky
ID: 41855689
It's your job to close the question ;)
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41855903
It looks like this is an assignment question. It is one thing to get an answer that works but the key thing is do you understand

a) Why it works
b) What steps were used to derive it

If the answer to either of these questions is no then you have not gained anything from the answer.

If you are learning to code it is more important that you understand the process than you get the right answer to questions such as these. The questions are designed to make you think about the problem you need to solve and there is not one correct answer. There are usually many approaches to solving a problem of this nature - some are better than others but in most cases it is the context that defines the best solution.

For instance you could have a solution like the one below.
What can we take out of this solution that is different from the one above
1. First we are evaluating validity in one if statement using Boolean operators between the conditions. Sometimes we want to do this to avoid complex nested if statements which have been shown to have a direct correlation to the number of bugs in a piece of code
2. We also see a function that has a default return and an override. We assume the combination of numbers is invalid - refer the "return false" at the end of the function. We override this default only if we find a valid combination of numbers.
3. Instead of returning a message we are returning a true or false value - this allows us more flexibility in the way we call this function and how we deal with the response. For instance if we want to call the function from different places and have a different message in each case. If we return the message from the function we loose the ability to do that.

There is nothing wrong with the solution above - but because this appears to be a learning question it is important to see the problem from the perspective of how to get to the solution rather than just finding a correct answer.

<script>
function validate(num1, num2, num3, num4)
{
	if (
     (num1 == 3 || num1 == 5 || num1 == 7) && // Condition 1
     (num2 == 2) &&                           // Condition 2
     (num3 >= 5 && num3 <= 100) &&             // Condition 3
     (num4 <= 9 ||num4 >= 20))                // Condition 4
		return true;
	return false
}
if (validate(3,2,10,3)) {
	console.log("Valid");
}
else {
	console.log("Invalid");
}
if (validate(11,2,10,3)) {
	console.log("Valid");
}
else {
	console.log("Invalid");
}
if (validate(3,3,10,3)) {
	console.log("Valid");
}
else {
	console.log("Invalid");
}
if (validate(3,2,5,3)) {
	console.log("Valid");
}
else {
	console.log("Invalid");
}
if (validate(3,2,3,3)) {
	console.log("Valid");
}
else {
	console.log("Invalid");
}
if (validate(3,2,10,20)) {
	console.log("Valid");
}
else {
	console.log("Invalid");
}
if (validate(3,2,10,15)) {
	console.log("Valid");
}
else {
	console.log("Invalid");
}

</script>

Open in new window

0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question