Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading