• Status: Solved
• Priority: Medium
• Security: Public
• Views: 100

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

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
• 2
1 Solution

Solutions ArchitectCommented:
``````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 Commented:
thank you so much you can close the question it worked.
0

Solutions ArchitectCommented:
It's your job to close the question ;)
0

Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.