Solved

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

Posted on 2016-10-22
4
42 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
Comment Utility
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
Comment Utility
thank you so much you can close the question it worked.
0
 
LVL 10

Expert Comment

by:Michael Vasilevsky
Comment Utility
It's your job to close the question ;)
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now