简体   繁体   中英

How to display different text on each week of the year

I have an environmental blog and I am working on this gadget to display an environmental fact each week of the year. I have tested out individual sections of the program to make sure it works, however, when I combine the sections it does not work. I have not finished putting all of the facts in because of this. Here is my program`

<script>
    var fact = new Array();
    fact[1] = "Only 1% of our planet’s water supply can be used. 97% is ocean water and 2% is frozen solid in the Arctic, for now.";
    fact[2] = "Aluminum can be recycled continuously, as in forever. Recycling 1 aluminum can save enough energy to run our TVs for at least 3 hours. 80 trillion aluminum cans are used by humans every year."; 
    fact[3] = "Rainforests are cut down at a rate of 100 acres per minute.";
    fact[4] = "In the past 50 years, humans have consumed more resources than in all previous history."; 
    fact[5] = "More than 100 billion pieces of junk mail are delivered in the United States each year, which comes out to 848 pieces per household. The production, distribution and disposal of all that junk mail creates over 51 million metric tons of greenhouses gases annually, the equivalent emissions of more than 9.3 million cars. "; 
    fact[7] = "Air pollution is the fourth largest risk factor for premature deaths."; 
    fact[8] = "Making copy paper from 100% recycled content fiber instead of 100% virgin forest fibers reduces total energy consumption by 44%, net greenhouse gas emissions by 38%, particulate emissions by 41%, wastewater by 50%, solid waste by 49% and wood use by 100%."; 
    fact[9] = ""; 
    fact[10] = ""; 
    fact[11] = ""; 
    fact[12] = ""; 
    fact[13] = ""; 
    fact[14] = ""; 
    fact[15] = ""; 
    fact[16] = ""; 
    fact[17] = ""; 
    fact[18] = ""; 
    fact[19] = ""; 
    fact[20] = ""; 
    fact[21] = ""; 
    fact[22] = ""; 
    fact[23] = ""; 
    fact[24] = ""; 
    fact[25] = ""; 
    fact[26] = ""; 
    fact[27] = ""; 
    fact[28] = ""; 
    fact[29] = ""; 
    fact[30] = ""; 
    fact[31] = ""; 
    fact[32] = "";
    fact[33] = "";
    fact[34] = "";
    fact[35] = "";
    fact[36] = "";
    fact[37] = "";
    fact[38] = "";
    fact[39] = "";
    fact[40] = "";
    fact[41] = "";
    fact[42] = "";
    fact[43] = "";
    fact[44] = "";
    fact[45] = "";
    fact[46] = "";
    fact[47] = "";
    fact[48] = "";
    fact[49] = "";
    fact[50] = "";
    fact[51] = "";
    fact[52] = "";
    Date.prototype.getWeekNumber = function(){
      var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
      var dayNum = d.getUTCDay() || 7;
      d.setUTCDate(d.getUTCDate() + 4 - dayNum);
      var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
      return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
      stuff.innerHTML = fact[yearStart]
    };
      stuff = document.getElementById('today'); 
<script/>
<p id="green">Today's Earth Fact Is:  <span id="today"> </span> </p>

` Thanks!

The line stuff.innerHTML = fact[yearStart] will never execute as it is after the return statement of the function.

Perhaps a bad copy/paste but it should be after the stuff = document.getElementById('today');

You should also not do the assignment from inside the getWeekNumber . That method should just return the week number as its title implies.

 var fact = new Array(); fact[1] = "Only 1% of our planet's water supply can be used. 97% is ocean water and 2% is frozen solid in the Arctic, for now."; fact[2] = "Aluminum can be recycled continuously, as in forever. Recycling 1 aluminum can save enough energy to run our TVs for at least 3 hours. 80 trillion aluminum cans are used by humans every year."; fact[3] = "Rainforests are cut down at a rate of 100 acres per minute."; fact[4] = "In the past 50 years, humans have consumed more resources than in all previous history."; fact[5] = "More than 100 billion pieces of junk mail are delivered in the United States each year, which comes out to 848 pieces per household. The production, distribution and disposal of all that junk mail creates over 51 million metric tons of greenhouses gases annually, the equivalent emissions of more than 9.3 million cars. "; fact[7] = "Air pollution is the fourth largest risk factor for premature deaths."; fact[8] = "Making copy paper from 100% recycled content fiber instead of 100% virgin forest fibers reduces total energy consumption by 44%, net greenhouse gas emissions by 38%, particulate emissions by 41%, wastewater by 50%, solid waste by 49% and wood use by 100%."; fact[9] = ""; fact[10] = ""; fact[11] = ""; fact[12] = ""; fact[13] = ""; fact[14] = ""; fact[15] = ""; fact[16] = ""; fact[17] = ""; fact[18] = ""; fact[19] = ""; fact[20] = ""; fact[21] = ""; fact[22] = ""; fact[23] = ""; fact[24] = ""; fact[25] = ""; fact[26] = ""; fact[27] = ""; fact[28] = ""; fact[29] = ""; fact[30] = ""; fact[31] = ""; fact[32] = ""; fact[33] = ""; fact[34] = ""; fact[35] = ""; fact[36] = ""; fact[37] = ""; fact[38] = ""; fact[39] = ""; fact[40] = ""; fact[41] = ""; fact[42] = ""; fact[43] = ""; fact[44] = ""; fact[45] = ""; fact[46] = ""; fact[47] = ""; fact[48] = "week 48"; fact[49] = "week 49"; fact[50] = "week 50"; fact[51] = "week 51"; fact[52] = "week 52"; Date.prototype.getWeekNumber = function() { var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate())), dayNum = d.getUTCDay() || 7, yearStart; d.setUTCDate(d.getUTCDate() + 4 - dayNum); yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)); return Math.ceil((((d - yearStart) / 86400000) + 1) / 7) }; var stuff = document.getElementById('today'), weekNumber = (new Date()).getWeekNumber(); stuff.innerHTML = fact[weekNumber]; 
 <p id="green">Today's Earth Fact Is: <span id="today"> </span> </p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM