英文:
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("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"));
<!-- 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;`
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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"));
<!-- end snippet -->
英文:
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("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"));
<!-- end snippet -->
答案2
得分: 0
当您创建一个插值字符串时,${}
中的值会立即被计算,它们不会在以后使用字符串时被“记住”。在定义 link
时,codeShort
和 codeLong
都是未定义的。
不要在函数开始时定义 link
,而是首先执行逻辑来计算 codeShort
和 codeLong
,然后再定义 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论