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:
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"));
What I want to return would look something like: ‘Creative Commons Attribution-NonCommercial License‘
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
>Solution :
Wrong order of operations and missing destructuring let [test] = codeToTest;
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"));