Counting time in JavaScript
I have this little riddle that I want to solve, this is eavesdropping on me, this is a riddle:
January 1, 1900 Thirty days - September, April, June and November. Everything else is thirty-one, just a saving February, which has twenty-eight, rain or shine. And for leap years, twenty-nine. A leap year occurs in any year evenly divisible by 4, but not by a century, unless it is divisible by 400. How many Sundays fell on the first of the month during the twentieth century (January 1, 1901 to December 31, 2000)?
THE CODE I DID WHAT:
var sundays = 0;
for (var year = 1901; year <= 2000; year++) {
for (var month = 1; month <= 12; month++) {
}
}
WHAT I NEED:
I'm not sure what this would be in order to solve this problem?
Thanks, please help me.
source to share
You can create one Date object and increment the year and month:
var from=new Date(),
m= 0,
y= 1901,
A= [];
while(y<2001){
if(m== 11){
m= 0;
++y;
}
from.setFullYear(y,m++,1);
if(from.getDay()=== 0) A.push(from.toLocaleDateString());
}
A.length+' Sundays begin months between 1901 and 2000.\n'+
A.join('; ');
/ * return value:
156 The months between 1901 and 2000 begin on Sundays.
9/1/1901; 6/1/1902; 2/1/1903; 3/1/1903; 11/1/1903; 5/1/1904; 1/1/1905; 10/1/1905; 4/1/1906; 7/1/1906; 9/1/1907; 3/1/1908; 11/1/1908; 8/1/1909; 5/1/1910; 1/1/1911; 10/1/1911; 9/1/1912; 6/1/1913; 2/1/1914; 3/1/1914; 11/1/1914; 8/1/1915; 10/1/1916; 4/1/1917; 7/1/1917; 9/1/1918; 6/1/1919; 2/1/1920; 8/1/1920; 5/1/1921; 1/1/1922; 10/1/1922; 4/1/1923; 7/1/1923; 6/1/1924; 2/1/1925; 3/1/1925; 11/1/1925; 8/1/1926; 5/1/1927; 1/1/1928; 4/1/1928; 7/1/1928; 9/1/1929; 6/1/1930; 2/1/1931; 3/1/1931; 11/1/1931; 5/1/1932; 1/1/1933; 10/1/1933; 4/1/1934; 7/1/1934; 9/1/1935; 3/1/1936; 11/1/1936; 8/1/1937; 5/1/1938; 1/1/1939; 10/1/1939; 9/1/1940; 6/1/1941; 2/1/1942; 3/2/1942; 11/2/1942; 8/1/1943; 10/1/1944; 4/1/1945; 7/1/1945; 9/1/1946; 6/1/1947; 2/1/1948; 8/1/1948; 5/1/1949; 1/1/1950; 10/1/1950; 4/1/1951; 7/1/1951; 6/1/1952; 2/1/1953; 3/1/1953; 11/1/1953; 8/1/1954;5/1/1955; 1/1/1956; 4/1/1956; 7/1/1956; 9/1/1957; 6/1/1958; 2/1/1959; 3/1/1959; 11/1/1959; 5/1/1960; 1/1/1961; 10/1/1961; 4/1/1962; 7/1/1962; 9/1/1963; 3/1/1964; 11/1/1964; 8/1/1965; 5/1/1966; 1/1/1967; 10/1/1967; 9/1/1968; 6/1/1969; 2/1/1970; 3/1/1970; 11/1/1970; 8/1/1971; 10/1/1972; 4/1/1973; 7/1/1973; 9/1/1974; 6/1/1975; 2/1/1976; 8/1/1976; 5/1/1977; 1/1/1978; 10/1/1978; 4/1/1979; 7/1/1979; 6/1/1980; 2/1/1981; 3/1/1981; 11/1/1981; 8/1/1982; 5/1/1983; 1/1/1984; 4/1/1984; 7/1/1984; 9/1/1985; 6/1/1986; 2/1/1987; 3/1/1987; 11/1/1987; 5/1/1988; 1/1/1989; 10/1/1989; 4/1/1990; 7/1/1990; 9/1/1991; 3/1/1992; 11/1/1992; 8/1/1993; 5/1/1994; 1/1/1995; 10/1/1995; 9/1/1996; 6/1/1997; 2/1/1998; 3/1/1998; 11/1/1998; 8/1/1999; 10/1/20007/1/1962; 9/1/1963; 3/1/1964; 11/1/1964; 8/1/1965; 5/1/1966; 1/1/1967; 10/1/1967; 9/1/1968; 6/1/1969; 2/1/1970; 3/1/1970; 11/1/1970; 8/1/1971; 10/1/1972; 4/1/1973; 7/1/1973; 9/1/1974; 6/1/1975; 2/1/1976; 8/1/1976; 5/1/1977; 1/1/1978; 10/1/1978; 4/1/1979; 7/1/1979; 6/1/1980; 2/1/1981; 3/1/1981; 11/1/1981; 8/1/1982; 5/1/1983; 1/1/1984; 4/1/1984; 7/1/1984; 9/1/1985; 6/1/1986; 2/1/1987; 3/1/1987; 11/1/1987; 5/1/1988; 1/1/1989; 10/1/1989; 4/1/1990; 7/1/1990; 9/1/1991; 3/1/1992; 11/1/1992; 8/1/1993; 5/1/1994; 1/1/1995; 10/1/1995; 9/1/1996; 6/1/1997; 2/1/1998; 3/1/1998; 11/1/1998; 8/1/1999; 10/1/20007/1/1962; 9/1/1963; 3/1/1964; 11/1/1964; 8/1/1965; 5/1/1966; 1/1/1967; 10/1/1967; 9/1/1968; 6/1/1969; 2/1/1970; 3/1/1970; 11/1/1970; 8/1/1971; 10/1/1972; 4/1/1973; 7/1/1973; 9/1/1974; 6/1/1975; 2/1/1976; 8/1/1976; 5/1/1977; 1/1/1978; 10/1/1978; 4/1/1979; 7/1/1979; 6/1/1980; 2/1/1981; 3/1/1981; 11/1/1981; 8/1/1982; 5/1/1983; 1/1/1984; 4/1/1984; 7/1/1984; 9/1/1985; 6/1/1986; 2/1/1987; 3/1/1987; 11/1/1987; 5/1/1988; 1/1/1989; 10/1/1989; 4/1/1990; 7/1/1990; 9/1/1991; 3/1/1992; 11/1/1992; 8/1/1993; 5/1/1994; 1/1/1995; 10/1/1995; 9/1/1996; 6/1/1997; 2/1/1998; 3/1/1998; 11/1/1998; 8/1/1999; 10/1/20007/1/1979; 6/1/1980; 2/1/1981; 3/1/1981; 11/1/1981; 8/1/1982; 5/1/1983; 1/1/1984; 4/1/1984; 7/1/1984; 9/1/1985; 6/1/1986; 2/1/1987; 3/1/1987; 11/1/1987; 5/1/1988; 1/1/1989; 10/1/1989; 4/1/1990; 7/1/1990; 9/1/1991; 3/1/1992; 11/1/1992; 8/1/1993; 5/1/1994; 1/1/1995; 10/1/1995; 9/1/1996; 6/1/1997; 2/1/1998; 3/1/1998; 11/1/1998; 8/1/1999; 10/1/20007/1/1979; 6/1/1980; 2/1/1981; 3/1/1981; 11/1/1981; 8/1/1982; 5/1/1983; 1/1/1984; 4/1/1984; 7/1/1984; 9/1/1985; 6/1/1986; 2/1/1987; 3/1/1987; 11/1/1987; 5/1/1988; 1/1/1989; 10/1/1989; 4/1/1990; 7/1/1990; 9/1/1991; 3/1/1992; 11/1/1992; 8/1/1993; 5/1/1994; 1/1/1995; 10/1/1995; 9/1/1996; 6/1/1997; 2/1/1998; 3/1/1998; 11/1/1998; 8/1/1999; 10/1/2000
* /
source to share
Your code is still fine, but now for each month, you need to check what day the first month was. You can use objects to achieve this goal Date
.
var sundays = 0;
for (var year = 1901; year<2001; year++) {
for (var month = 1; month<=12; month++) {
if (new Date(year, month-1, 1).getDay() == 0) {
sundays++;
}
}
}
As @Amadan pointed out, getDay returns 0 for Sunday.
source to share