سؤال

In a Javascript program to convert a Julian day to a calendar day, the following code is seen:

 month = (G<13.5) ? (G-1) : (G-13)
  year = (month<2.5) ? (C-4715) : (C-4716)

I thought these may have been some sort of conditions that changed the values of month or year according to the values of the variables within their declarations My research has returned nothing on these symbols--particularly the "?"--showing up in Javascript code.

For a complete view of the source, visit astronomy.villanova.edu/links/jd.htm

Any help would be greatly appreciated!

هل كانت مفيدة؟

المحلول

This is called the ternary or conditional operators in javascript

It is a short hand for something like:

if(a ==0){
   b = 1;
}
else{
   b = 0;
}

نصائح أخرى

variable = if condition is true ? give this answer : otherwise give this one;

so int x = 5; int y = x==5? 10 :12; would mean if x is equal to 5, y would be 10 , otherwise it would be 12

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top