Jump to content

how do I add more than one condition to an "if " statement ?


ncc1701d

Recommended Posts

how do I add more than one condition to an "if " statement? for example

i want to say if i < 42 and i is multple of 4 and is an odd number then print?

how do i write that in javascript?

or say i have
i = 1951 // i would change value for i around to test if leap year or not
if (i=1952 or 1956 or 1960 or 1964) //this does not work though
{
core.debug("oops this is leap year");
}
else
{
core.debug("this is not leap year");
}


thanks.

Link to comment
Share on other sites

Like this?

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script>
function IfLeapYear(year)
{
  return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

var startYear=1952;
var join="";

function checkYear(){
for(i=0;i<42;i++)
{
if(IfLeapYear(startYear)){
join+=startYear + " : is Leap year<br>";
}
else
{
join+=startYear + " : is NOT a Leap year<br>";
}
startYear=startYear+4;
}
document.getElementById("years").innerHTML=join;
}

window.onload=checkYear;
</script>

</head>
<body>

<h1>This is a Heading</h1>
<p id="years">leap year or not</p>

</body>
</html>
Link to comment
Share on other sites

thanks iam sure that works for web page but i was hoping for something more simple in the way it was worded like the way i wrote it

so i can incorporate it with my project which uses javascript script language in limited ways but is not for a webpage or browser.

Iam a beginner.

return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);// like i have no idea what this line is even saying.

maybe you can translate it in english? ;)

Link to comment
Share on other sites

It calculates using modulus(%) if given year when divided by 4 leaves no remainder AND when divided by 100 the remainder is not equal to 0, OR when divided by 400 the remainder equals 0.

 

This will provide a true or false result if leapyear value or not.

 

if(IfLeapYear(startYear)) checks if returned result from function is true, if true, carry out following process, the else carries out the following the process if returned result is false.

 

So instead of 42 if comparisons of leap year values to known leap years, the function calclates if value is of a leap year value and compares a single value of true or false.

 

In the Gregorian calendar three criteria must be taken into account to identify leap years:

 

The year can be evenly divided by 4;

If the year can be evenly divided by 100, it is NOT a leap year, unless;

The year is also evenly divisible by 400. Then it is a leap year.

 

Why? "If the year can be evenly divided by 100, it is NOT a leap year" because 3 times out of 400 years the leap year is skipped, so for example 2100, 2200, 2300 won't be leap years.

Edited by dsonesuk
Link to comment
Share on other sites

It would have been

if (i==1952 or i==1956 or i==1960 or i==1964 and on, and on and on 42 times, actually more considering you have allow when start that begins, so basically include all leap years from beginning of time to end of time to cover any start year, in one word 'ridiculous'.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...