首页 > 解决方案 > 我需要这些 JavaScript 数组中的每个第一句正确显示,并且正确地包含在其中的星期几

问题描述

我有这些 javascripts 来随机获取 2 个不同的句子,每个句子都来自一组不同的数组,当我在通知我们的成员关于我们对我们每个人的评论时,每次单击其打印文本(以避免垃圾邮件)时都会收到不同的消息讲座。

我想在第一句话中包含工作日,但是在尝试了几十种方法之后,我无法让它按我的需要工作。

到目前为止,我必须将工作日分开,将其剪切(ctrl+X),然后将其粘贴到我在此处标记为“工作日”的正确位置,以向您展示我需要它的位置。

我期望的结果是这样的:
2周六对你的两堂课发表评论。小心。

完成 2周六对您的每堂课的评论。我们回聊。


这是我的 HTML:
<div style="cursor: pointer;" onClick="setAleatoryNtfctnLines(); setAleatoryFarewell();">
    <div><span id="AleatoryNotification"></span> <span id="AleatoryFarewell"></span></div>
    <div><span id="weekday"></span></div>
</div>

这些是我的javascripts:
<script>
const FirstTextLines = [
"2 «weekday» comments done, for your 2 lectures",
"2 «weekday» comments done, on both your lectures",
"2 «weekday» comments for both your lectures",
"2 «weekday» comments for each of your lectures",
"2 «weekday» comments for your two lectures",
"2 «weekday» comments on both your lectures",
"2 «weekday» comments on each of your lectures",
"Already done 2 «weekday» comments x 2",
"Done 2 «weekday» comments on both your lectures",
"Done 2 «weekday» comments on each of your lectures",
"Done 2 «weekday» comments x 2",
"I just posted 2 «weekday» comments x 2",
"Just posted 2 «weekday» comments x 2"
];

const getAleatoryLines = max => Math.floor(Math.random() * max); 

const getAleatoryNtfctnLines = () => `${FirstTextLines[getAleatoryLines(FirstTextLines.length)]}`;

const setAleatoryNtfctnLines = () => {
  document.getElementById('AleatoryNotification').innerText = getAleatoryNtfctnLines();
};

document.getElementById('GenerateFirstSetOfLines').
addEventListener('click', setAleatoryNtfctnLines);

setAleatoryNtfctnLines();
</script>

<script>
var d = new Date();
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
document.getElementById("weekday").innerHTML = days[d.getDay()];
</script>

<script>
const SecondTextLines = [
"Bye bye!",
"Bye for now.",
"Catch you later.",
"Have a good one.",
"Have a nice day.",
"Peace out.",
"Soon with more.",
"Take care.",
"Talk to you later.",
"Until next time."
];

const getAleatoryFarewellLines = max => Math.floor(Math.random() * max);

const getAleatoryFarewell = () => `${SecondTextLines[getAleatoryFarewellLines(SecondTextLines.length)]}`;

const setAleatoryFarewell = () => {
  document.getElementById('AleatoryFarewell').innerText = getAleatoryFarewell();
};

document.getElementById('GenerateSecondSetOfLines').
addEventListener('click', setAleatoryFarewell);

setAleatoryFarewell();
</script>

我知道在尝试了太多次之后我有几个错误,但修复这些错误并不是我想要的。

我想要的是在第一句话中包含工作日,如我之前的示例所示。

如果您建议我采用完全不同的方法,那也没关系。
如果我无法获得所需的结果,我愿意接受任何建议。

这是我之前制作的一个jsfiddle,向您展示我的意思。

先感谢您。

标签: javascripthtmljquery

解决方案


尝试将变量替换与模板文字一起使用。

const date = new Date();
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const day = days[date.getDay()];

const FirstTextLines = [
  `2 ${day} comments done, for your 2 lectures`,
  `2 ${day} comments done, on both your lectures`,
  `2 ${day} comments for both your lectures`,
  `2 ${day} comments for each of your lectures`,
  `2 ${day} comments for your two lectures`,
  `2 ${day} comments on both your lectures`,
  `2 ${day} comments on each of your lectures`,
  `Already done 2 ${day} comments x 2`,
  `Done 2 ${day} comments on both your lectures`,
  `Done 2 ${day} comments on each of your lectures`,
  `Done 2 ${day} comments x 2`,
  `I just posted 2 ${day} comments x 2`,
  `Just posted 2 ${day} comments x 2`
];

console.log(FirstTextLines);


推荐阅读