尝试将变量插入字符串,但在JavaScript中始终得到未定义。

huangapple go评论52阅读模式
英文:

Trying to insert variables into a string, keep getting undefined with JavaScript

问题

以下是代码部分的翻译:

function generateLicenseLink(licenseCode) {
  let code = licenseCode.replace("CC-", "").toLowerCase();
  let codeToTest = code.split(/-{3}/);
  let test = codeToTest;
  let codeShort;
  let codeLong;

  let link = `<a href="https://creativecommons.org/licenses/${codeShort}/4.0/">${codeLong}</a>`;

  if (test === "by") {
    codeShort = "by";
    codeLong = "Creative Commons Attribution License";
  } else if (test === "by-nc") {
    codeShort = "by-nc";
    codeLong = "Creative Commons Attribution-NonCommercial License";
  } else if (test === "by-sa") {
    codeShort = "by-sa";
    codeLong = "Creative Commons Attribution-ShareAlike License";
  } else if (test === "by-nd") {
    codeShort = "by-nd";
    codeLong = "Creative Commons Attribution-NoDerivs License";
  } else if (test === "by-nd-sa") {
    codeShort = "by-nd-sa";
    codeLong = "Creative Commons Attribution-NonCommercial-ShareAlike License";
  } else if (test === "by-nc-nd") {
    codeShort = "by-nc-nd";
    codeLong = "Creative Commons Attribution-NonCommercial-NoDerivs License";
  }

  return link;
}

console.log(generateLicenseLink("CC-BY"));
console.log(generateLicenseLink("CC-BY-NC"));
console.log(generateLicenseLink("CC-BY-SA"));
console.log(generateLicenseLink("CC-BY-ND"));
console.log(generateLicenseLink("CC-BY-NC-SA"));
console.log(generateLicenseLink("CC-BY-NC-ND"));

希望这个翻译对你有所帮助。

英文:

I'm trying to make a function that accepts a license code and then returns a link element with the proper license code and description. I can't seem to figure out where I went wrong in the code, but I'm guessing in the if statements.

This is what I've got:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function generateLicenseLink(licenseCode) {
  let code = licenseCode.replace(&quot;CC-&quot;, &quot;&quot;).toLowerCase();
  let codeToTest = code.split(/-{3}/);
  let test = codeToTest;
  let codeShort;
  let codeLong;

  let link = `&lt;a href =&quot;https://creativecommons.org/licenses/${codeShort}/4.0/&gt;${codeLong}&lt;/a&gt;`;

  if (test === &quot;by&quot;) {
    codeShort = &quot;by&quot;;
    codeLong = &quot;Creative Commons Attribution License&quot;;
  } else if (test === &quot;by-nc&quot;) {
    codeShort = &quot;by-nc&quot;;
    codeLong = &quot;Creative Commons Attribution-NonCommercial License&quot;;
  } else if (test === &quot;by-sa&quot;) {
    codeShort = &quot;by-sa&quot;;
    codeLong = &quot;Creative Commons Attribution-ShareAlike License&quot;;
  } else if (test === &quot;by-nd&quot;) {
    codeShort = &quot;by-nd&quot;;
    codeLong = &quot;Creative Commons Attribution-NoDerivs License&quot;;
  } else if (test === &quot;by-nd-sa&quot;) {
    codeShort = &quot;by-nd-sa&quot;;
    codeLong = &quot;Creative Commons Attribution-NonCommercial-ShareAlike License&quot;;
  } else if (test === &quot;by-nc-nd&quot;) {
    codeShort = &quot;by-nc-nd&quot;;
    codeLong = &quot;Creative Commons Attribution-NonCommercial-NoDerivs License&quot;;
  }

  return link;
}

console.log(generateLicenseLink(&quot;CC-BY&quot;));
console.log(generateLicenseLink(&quot;CC-BY-NC&quot;));
console.log(generateLicenseLink(&quot;CC-BY-SA&quot;));
console.log(generateLicenseLink(&quot;CC-BY-ND&quot;));
console.log(generateLicenseLink(&quot;CC-BY-NC-SA&quot;));
console.log(generateLicenseLink(&quot;CC-BY-NC-ND&quot;));

<!-- end snippet -->

What I want to return would look something like: '<a href="https://creativecommons.org/licenses/by-nc/4.0/">Creative Commons Attribution-NonCommercial License</a>'

Thank you!

I've tried using switch instead of if statement, I've tried to format the URL differently, and tried to replace the "CC-" and split the elements at different points. It's all just coming up as undefined in the variables section, as such:

<a href ="https://creativecommons.org/licenses/undefined/4.0/>undefined</a>

答案1

得分: 1

错误的操作顺序和缺少解构 `let [test] = codeToTest;`

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    function generateLicenseLink(licenseCode) {
      let code = licenseCode.replace("CC-", "").toLowerCase();
      let codeToTest = code.split(/-{3}/);
      let [test] = codeToTest;
      let codeShort;
      let codeLong;

      if (test === "by") {
        codeShort = "by";
        codeLong = "Creative Commons Attribution License";
      } else if (test === "by-nc") {
        codeShort = "by-nc";
        codeLong = "Creative Commons Attribution-NonCommercial License";
      } else if (test === "by-sa") {
        codeShort = "by-sa";
        codeLong = "Creative Commons Attribution-ShareAlike License";
      } else if (test === "by-nd") {
        codeShort = "by-nd";
        codeLong = "Creative Commons Attribution-NoDerivs License";
      } else if (test === "by-nd-sa") {
        codeShort = "by-nd-sa";
        codeLong = "Creative Commons Attribution-NonCommercial-ShareAlike License";
      } else if (test === "by-nc-nd") {
        codeShort = "by-nc-nd";
        codeLong = "Creative Commons Attribution-NonCommercial-NoDerivs License";
      }

      let link = `<a href ="https://creativecommons.org/licenses/${codeShort}/4.0/">${codeLong}</a>`;

      return link;
    }

    console.log(generateLicenseLink("CC-BY"));
    console.log(generateLicenseLink("CC-BY-NC"));
    console.log(generateLicenseLink("CC-BY-SA"));
    console.log(generateLicenseLink("CC-BY-ND"));
    console.log(generateLicenseLink("CC-BY-NC-SA"));
    console.log(generateLicenseLink("CC-BY-NC-ND"));

&lt;!-- end snippet --&gt;
英文:

Wrong order of operations and missing destructuring let [test] = codeToTest;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function generateLicenseLink(licenseCode) {
let code = licenseCode.replace(&quot;CC-&quot;, &quot;&quot;).toLowerCase();
let codeToTest = code.split(/-{3}/);
let [test] = codeToTest;
let codeShort;
let codeLong;
if (test === &quot;by&quot;) {
codeShort = &quot;by&quot;;
codeLong = &quot;Creative Commons Attribution License&quot;;
} else if (test === &quot;by-nc&quot;) {
codeShort = &quot;by-nc&quot;;
codeLong = &quot;Creative Commons Attribution-NonCommercial License&quot;;
} else if (test === &quot;by-sa&quot;) {
codeShort = &quot;by-sa&quot;;
codeLong = &quot;Creative Commons Attribution-ShareAlike License&quot;;
} else if (test === &quot;by-nd&quot;) {
codeShort = &quot;by-nd&quot;;
codeLong = &quot;Creative Commons Attribution-NoDerivs License&quot;;
} else if (test === &quot;by-nd-sa&quot;) {
codeShort = &quot;by-nd-sa&quot;;
codeLong = &quot;Creative Commons Attribution-NonCommercial-ShareAlike License&quot;;
} else if (test === &quot;by-nc-nd&quot;) {
codeShort = &quot;by-nc-nd&quot;;
codeLong = &quot;Creative Commons Attribution-NonCommercial-NoDerivs License&quot;;
}
let link = `&lt;a href =&quot;https://creativecommons.org/licenses/${codeShort}/4.0/&gt;${codeLong}&lt;/a&gt;`;
return link;
}
console.log(generateLicenseLink(&quot;CC-BY&quot;));
console.log(generateLicenseLink(&quot;CC-BY-NC&quot;));
console.log(generateLicenseLink(&quot;CC-BY-SA&quot;));
console.log(generateLicenseLink(&quot;CC-BY-ND&quot;));
console.log(generateLicenseLink(&quot;CC-BY-NC-SA&quot;));
console.log(generateLicenseLink(&quot;CC-BY-NC-ND&quot;));

<!-- end snippet -->

答案2

得分: 0

当您创建一个插值字符串时,${} 中的值会立即被计算,它们不会在以后使用字符串时被“记住”。在定义 link 时,codeShortcodeLong 都是未定义的。

不要在函数开始时定义 link,而是首先执行逻辑来计算 codeShortcodeLong,然后再定义 link

英文:

When you create an interpolated string, the values in the ${} are evaluated immediately, they aren't "remembered" for when you later use the string. At the time you defined link, codeShort and codeLong were undefined.

Instead of defining link at the beginning of the function, first perform the logic to calculate codeShort and codeLong, and then only then define link.

huangapple
  • 本文由 发表于 2023年2月18日 02:07:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定