Solved

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

Posted on 2016-10-22
79 Views
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
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 10

Accepted Solution

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);
``````
0

Author Comment

ID: 41855680
thank you so much you can close the question it worked.
0

LVL 10

Expert Comment

ID: 41855689
It's your job to close the question ;)
0

LVL 58

Expert Comment

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>
``````
0

## Featured Post

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
###### Suggested Courses
Course of the Month10 days, 18 hours left to enroll