Сначала позвольте мне сказать спасибо всем, кто ответил. Ваше понимание помогло мне понять, что я делаю неправильно.
Я нашел ответ в ответе Навед-хана и Ферена c. Я подумал, что нужна математика с годами и месяцами, но не мог придумать, какую формулу использовать. Я использовал формулу в методе Khan's monthDiff (), так как предпочел разбить формулу на две части.
var diff = todayDate.getMonth() - startDate.getMonth() + (12 * (todayDate.getFullYear() - startDate.getFullYear()));
Как только он закончил, как он и сделал в своем ответе, я назначил diff как индекс GreetingDetails. .. GreetingDetails [diff] и альт, волхвы c случились. Это немного сработало ... пока я не добрался до последних 20 лет, потом я начал получать неправильное приветствие. Проблема была в том, что я oop в ответе Хана. Поэтому я изменил его на вычитание GreetingDetails.length (длина 20), и каждый раз получаю правильное приветствие.
while (diff >= GreetingDetails.length) {
diff -= GreetingDetails.length;
}
Это также поможет позже, если число приветствий в GreetingDetails [] изменится. Мне нужно, чтобы это было как можно более динамичным c, так что это должен быть лучший способ сделать это как таковой. Таким образом, нужно будет изменить только детали приветствия и ничего больше.
Хотя Хан и Ферен c были слегка отклонены от формулы, я голосую за них, чтобы отдать им должное, как я использовал их ответы, чтобы сформулировать решение моей проблемы.
Полный JS ниже:
var todayDate = new Date();
var startDate = new Date(2020, 0)
var diff = todayDate.getMonth() - startDate.getMonth() + (12 * (todayDate.getFullYear() - startDate.getFullYear()));
var link = "url";
var GreetingBuilder;
var GreetingDetails = [
{// index0
Greeting: "Foreign Greeting 0",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation0.mp3",
CommerceLink: link
}, // index0
{// index1
Greeting: "Foreign Greeting 1",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation1.mp3",
CommerceLink: link
}, // index1
{// index2
Greeting: "Foreign Greeting 2",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation2.mp3",
CommerceLink: link
}, // index2
{// index3
Greeting: "Foreign Greeting 3",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation3.mp3",
CommerceLink: link
}, // index3
{// index4
Greeting: "Foreign Greeting 4",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation4.mp3",
CommerceLink: link
}, // index4
{// index5
Greeting: "Foreign Greeting 5",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation5.mp3",
CommerceLink: link
}, // index5
{// index6
Greeting: "Foreign Greeting 6",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation6.mp3",
CommerceLink: link
}, // index6
{// index7
Greeting: "Foreign Greeting 7",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation7.mp3",
CommerceLink: link
}, // index7
{// index8
Greeting: "Foreign Greeting 8",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation8.mp3",
CommerceLink: link
}, // index8
{// index9
Greeting: "Foreign Greeting 9",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation9.mp3",
CommerceLink: link
}, // index9
{// index10
Greeting: "Foreign Greeting 10",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation10.mp3",
CommerceLink: link
}, // index10
{// index11
Greeting: "Foreign Greeting 11",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation11.mp3",
CommerceLink: link
}, // index11
{// index12
Greeting: "Foreign Greeting 12",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation12.mp3",
CommerceLink: link
}, // index12
{// index13
Greeting: "Foreign Greeting 13",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation13.mp3",
CommerceLink: link
}, // index13
{// index14
Greeting: "Foreign Greeting 14",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation14.mp3",
CommerceLink: link
}, // index14
{// index15
Greeting: "Foreign Greeting 15",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation15.mp3",
CommerceLink: link
}, // index15
{// index16
Greeting: "Foreign Greeting 16",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation16.mp3",
CommerceLink: link
}, // index16
{// index17
Greeting: "Foreign Greeting 17",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation17.mp3",
CommerceLink: link
}, // index17
{// index18
Greeting: "Foreign Greeting 18",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation18.mp3",
CommerceLink: link
}, // index18
{// index19
Greeting: "Foreign Greeting 19",
Language: "Foreign Language",
Translation: "English Translation",
AudioPath: "/Content/Greetings/Pronunciation19.mp3",
CommerceLink: link
} // index19
];
while (diff >= GreetingDetails.length) {
diff -= GreetingDetails.length;
}
GreetingElements = {
topSection: function () {
var holder = document.createElement("section");
holder.id = "required-greeting";
return holder;
},
accessabilityP: function () {
var holder = document.createElement("p");
holder.classList.add("visuallyhidden");
holder.innerText = "The following is a greeting given in one of 20 foreign languages.";
return holder;
},
greetingContainer: function () {
var holder = document.createElement("dl");
holder.id = "language-greeting";
return holder;
},
greetingDT: function () {
var holder = document.createElement("dt");
holder.classList.add("greeting");
return holder;
},
greetingLink: function () {
var holder = document.createElement("a")
holder.href = GreetingBuilder.CommerceLink;
holder.classList.add("language-link");
holder.text = GreetingBuilder.Greeting;
return holder;
},
audioContainer: function () {
var holder = document.createElement("dd");
holder.classList.add("speaker");
return holder;
},
audioPlayer: function () {
var holder = document.createElement("audio");
holder.id = GreetingBuilder.Language.toLowerCase().replace(" ", "-");
holder.src = GreetingBuilder.AudioPath;
holder.preload = "auto";
return holder;
},
audioButton: function () {
var holder = document.createElement("button");
holder.classList.add("pronunciation");
holder.onclick = function () {
document.getElementById(GreetingElements.audioPlayer().id).play();
};
holder.innerText = "Listen to pronunciation";
return holder;
},
languageNameDD: function () {
var holder = document.createElement("dd");
holder.classList.add("language");
holder.innerText = "(" + GreetingBuilder.Language + ")";
return holder;
},
languageTranslationDD: function () {
var holder = document.createElement("dd");
holder.classList.add("translation");
holder.innerText = '"' + GreetingBuilder.Translation + '"';
return holder;
},
getFullGreetingSection: function () {
var sectionInstance = this.topSection();
var greetingDTInstance = this.greetingDT();
var greetingContainerInstance = this.greetingContainer();
var audioContainerInstance = this.audioContainer();
greetingDTInstance.appendChild(this.greetingLink());
greetingContainerInstance.appendChild(greetingDTInstance);
audioContainerInstance.appendChild(this.audioPlayer());
audioContainerInstance.appendChild(this.audioButton());
greetingContainerInstance.appendChild(audioContainerInstance);
greetingContainerInstance.appendChild(this.languageNameDD());
greetingContainerInstance.appendChild(this.languageTranslationDD());
sectionInstance.appendChild(this.accessabilityP());
sectionInstance.appendChild(greetingContainerInstance);
return sectionInstance;
}
}
GreetingBuilder = GreetingDetails[diff];
document.getElementsByClassName("back-to-top")[0].insertAdjacentElement('afterend', GreetingElements.getFullGreetingSection());
Я надеюсь, что это поможет кому-то в будущем!