优化代码以将数字 1 - 25 均匀映射到 5 种颜色



我目前正在学习JavaScript,我编写了这段代码(并且工作正常),但我知道必须有一种方法可以更好地自动化它。我对循环有点模糊,但根据我对它们的了解,我不知道在这种情况下可以使用一个:

function calculation() {
var num = document.getElementById("input").value;
var trCalc = num % 25;
var tipColorName = document.getElementById("tipColorName");
var ringColorName = document.getElementById("ringColorName");
var tipBlock = document.getElementById("tipBlock");
var ringBlock = document.getElementById("ringBlock");
// Pair Calculations
if (trCalc > 0 && trCalc <= 5) {
    tipColorName.innerHTML = "White";
    tipBlock.setAttribute("style", "background-color: #f2f2f2; color: #2f2f2f;");
}
if (trCalc > 5 && trCalc <= 10) {
    tipColorName.innerHTML = "Red";
    tipBlock.setAttribute("style", "background-color: #e24341; color: white;");
}
if (trCalc > 10 && trCalc <= 15) {
    tipColorName.innerHTML = "Black";
    tipBlock.setAttribute("style", "background-color: #2f2f2f; color: white;");
}
if (trCalc > 15 && trCalc <= 20) {
    tipColorName.innerHTML = "Yellow";
    tipBlock.setAttribute("style", "background-color: #f4ca37; color: #2f2f2f;");
}
if (trCalc > 20 && trCalc <= 24 || num % 25 == 0 && num > 0 ) {
    tipColorName.innerHTML = "Violet";
    tipBlock.setAttribute("style", "background-color: #844ae0; color: white;");
}
if ([1, 6, 11, 16, 21].includes(trCalc)) {
    ringColorName.innerHTML = "Blue";
    ringBlock.setAttribute("style", "background-color: #426ffe; color: white;");
}
if ([2, 7, 12, 17, 22].includes(trCalc)) {
    ringColorName.innerHTML = "Orange";
    ringBlock.setAttribute("style", "background-color: #f27f42; color: #2f2f2f;");
}
if ([3, 8, 13, 18, 23].includes(trCalc)) {
    ringColorName.innerHTML = "Green";
    ringBlock.setAttribute("style", "background-color: #25d366; color: white;");
}
if ([4, 9, 14, 19, 24].includes(trCalc)) {
    ringColorName.innerHTML = "Brown";
    ringBlock.setAttribute("style", "background-color: #917260; color: #2f2f2f;");
}
if ([5, 10, 15, 20 ].includes(trCalc) || num % 25 == 0 && num > 0 ) {
    ringColorName.innerHTML = "Slate";
    ringBlock.setAttribute("style", "background-color: grey; color: #2f2f2f;");
}

任何帮助不胜感激!

优化和缩小实际上是两回事。 查看此站点以进行缩小 http://closure-compiler.appspot.com/home。

就优化而言,您可以做几件事。

你可以考虑使用一个函数来改变你的样式。 这将清理您的代码并使其更具可读性。像这样:

changeStyles(innerColor,bgColor,color){
  ringColorName.innerHTML = innerColor;
  ringBlock.setAttribute("style", "background-color: "+bgColor+"; color: "+color+";") 
}

那么你的 if 语句将如下所示:

if ([1, 6, 11, 16, 21].includes(trCalc)) {
   changeStyles("White","#f2f2f2","#2f2f2f");
}

你可以看看模运算符。对于最后五个if语句,看起来您可以执行foo = trCalc % 5后跟一个switch(foo)。可能看起来像这样。

foo = trCalc % 5
switch(foo) {
    case 0:
        changeStyles("White","#f2f2f2","#2f2f2f");
        break;
    case 1:
        changeStyles("White","#f2f2f2","#2f2f2f");
        break;
    case 2:
        changeStyles("White","#f2f2f2","#2f2f2f");
        break;
    case 3:
        changeStyles("White","#f2f2f2","#2f2f2f");
        break;
    case 4:
        changeStyles("White","#f2f2f2","#2f2f2f");
        break;
}

当然,您需要将适当的颜色插入功能changeColors。希望这能给你一些想法。

以下是我将如何优化它:

const tipColorNames = ['White', 'Red', 'Black', 'Yellow', 'Violet'];
const tipStyles = [
  "background-color: #f2f2f2; color: #2f2f2f;",
  "background-color: #e24341; color: white;",
  "background-color: #2f2f2f; color: white;",
  "background-color: #f4ca37; color: #2f2f2f;",
  "background-color: #844ae0; color: white;",
];
const ringColorNames = ['Blue', 'Orange', 'Green', 'Brown', 'Slate'];
const ringStyles = [
  "background-color: #426ffe; color: white;",
  "background-color: #f27f42; color: #2f2f2f;",
  "background-color: #25d366; color: white;",
  "background-color: #917260; color: #2f2f2f;",
  "background-color: grey; color: #2f2f2f;",
];
const divisor = 5;
const numColors = tipColorNames.length;
function calculation() {
  var num = document.getElementById("input").value;
  var trCalc = num % (divisor * numColors);
  var tipColorName = document.getElementById("tipColorName");
  var ringColorName = document.getElementById("ringColorName");
  var tipBlock = document.getElementById("tipBlock");
  var ringBlock = document.getElementById("ringBlock");
  const tipColorIndex = trCalc === 0 && num > 0
    ? numColors - 1
    : (trCalc - 1) / divisor;
  tipColorName.innerHTML = tipColorNames[tipColorIndex];
  tipBlock.setAttribute('style', tipStyles[tipColorIndex]);
  const ringColorIndex = (num - 1) % numColors;
  ringColorName.innerHTML = ringColorNames[ringColorIndex];
  ringBlock.setAttribute('style', ringStyles[ringColorIndex]);
}

相关内容

  • 没有找到相关文章

最新更新