JavaScript Algorithms And Data Structures | Functions not defined

I have completed all five of the certification projects, and when I run my programs in codepen, they work, but when I run them in the freeCodeCamp IDE, the console has a message that reads: “Uncaught ReferenceError: search is not defined”, where “search” is my function. It is doing this for all five of my projects.
If you have encountered this error before, please help.

Thank you,
~ CoderBoss.

Hello,

could you please share at least one project’s code? It’s very hard to address the issue without it. Also, make sure your HTML code includes the <!DOCTYPE> declaration and <html> tag. CodePen might not require them, but freeCodeCamp does.
Happy coding.

Thank you for the suggestion, although it did not work. I have attached the code for my palindrome checker.
HTML:

<!DOCTYPE html>
<html>
<body>

<input id="text-input" type="text">
<button id="check-btn" onclick="palindrome()">Check</button>
<div id="result"></div>

</body>
</html>

JavaScript:

function palindrome() {

  let str = document.getElementById("text-input").value;
  let newStr = "";
  let backwardsStr = "";

  if (str == "") {

    alert("Please input a value.");
  } else {
    for (let i = 0; i < str.length; i++) {

      if (/^[a-zA-Z0-9]+$/.test(str[i])) {

        newStr = newStr.concat(str[i]);
      }
    }

  newStr = newStr.toLowerCase();

  for (let j = newStr.length - 1; j > -1; j--) {

    backwardsStr = backwardsStr.concat(newStr[j]);
  }

    if (newStr === backwardsStr) {

      document.getElementById("result").innerHTML = str + " is a palindrome.";
    } else {

      document.getElementById("result").innerHTML = str + " is a not palindrome.";
    }
  }
}

You need to link the script in your HTML

1 Like

Thank you so much.
I have completed four of the certification projects, but I am having problems with the cash register. The tests wont pass, but I ran the tests myself and everything looks good. Do you know what the problem is? I have attached my code below.

HTML:

<!DOCTYPE html>
<html>

<script src="script.js"></script>

<body>

<input id="cash" type="text">
<button id="purchase-btn" onclick="checkCashRegister()">Purchase</button>
<div id="change-due"></div>

</body>
</html>

JavaScript:

function checkCashRegister() {
  
  let price = 19.5;
  let cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]];
  let cash = document.getElementById("cash").value;
    
  if (cash < price) {
      
    alert("Customer does not have enough money to purchase the item.");
  } else {
    let values = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
    let changeDue = cash - price;
    let object = {status: "", change: []};
    let totalReturnableChange = 0;
    let returnableChange = [];
    let totalCashAdded = 0;
    
    if (changeDue == 0) {
      document.getElementById("change-due").innerHTML = "No change due - customer paid with exact cash.";  
    } else {
      for (let i = 0; i < cid.length; i++) {
        if (values[i][1] <= changeDue) {
          totalReturnableChange += cid[i][1];
          returnableChange.push(cid[i]);
        }
      }

      if (totalReturnableChange < changeDue) {
        object.status = "INSUFFICIENT_FUNDS";
      } else if (totalReturnableChange == changeDue) {
        object.status = "CLOSED";
        object.change = cid;
      } else {
        for (let j = 1; j <= returnableChange.length; j++) {
          if (totalCashAdded + returnableChange[returnableChange.length - j][1] <= changeDue) {
            object.status = "OPEN";
            object.change.push(returnableChange[returnableChange.length - j]);
            totalCashAdded += returnableChange[returnableChange.length - j][1];
          } else {
            for (let k = 1; k < returnableChange[returnableChange.length - j][1] / values[returnableChange.length - j][1]; k++) {
              if (totalCashAdded + returnableChange[returnableChange.length - j][1] - (values[returnableChange.length - j][1] * k) <= changeDue) {
                object.status = "OPEN";
                object.change.push([returnableChange[returnableChange.length - j][0], returnableChange[returnableChange.length - j][1] - (values[returnableChange.length - j][1] * k)]);
                totalCashAdded += returnableChange[returnableChange.length - j][1] -  (values[returnableChange.length - j][1] * k);
              }
            }
          }
        }

        for (let l = 0; l < object.change.length; l++) {
          if (object.change[l][1].toString().length > 4) {
            let numStr = object.change[l][1].toString()[0];
            numStr = numStr.concat(object.change[l][1].toString()[1]);
            numStr = numStr.concat(object.change[l][1].toString()[2]);
            numStr = numStr.concat(object.change[l][1].toString()[3]);
            object.change[l][1] = Number(numStr);
          }

          if (object.change[l][0] == "DIME") {
            object.change[l][1] = Number(object.change[l][1].toFixed(1));
          }
        }

        if (object.change[object.change.length - 1][0] == "PENNY") {
          object.change[object.change.length - 1][1] += 0.01;
        }

      } 
      
      document.getElementById("change-due").innerHTML = "Status: " + object.status + "<br>";  
    
      for (let i = 0; i < object.change.length; i++) {
        if (object.change[i][1] > 0) {
          document.getElementById("change-due").innerHTML += object.change[i][0] + ": $" + object.change[i][1] + "<br>";
        }
      }
    }
  }
}

Thank you,
~ CoderBoss.

These need to stay where they were originally

script is not within the body element.

It doesn’t have to be

well. i learned it here in the first javascript challanges for role playing game.
@JeremyLT

The script tag can be put either between the head tags or just before the closing body tag. Nevertheless, it is best practice to put the script tag before the closing body tag to reduce the page’s loading time.

1 Like

thank you very much for the information.

1 Like

The script can not be placed before the DOM elements its code is accessing. At least, not without using defer or async which doesn’t work in the fCC editor, or by wrapping the code in a load or DOMContentLoaded event.

Place it before the closing body tag for the fCC challenges.

1 Like

Thank you so much.
Because of all your help, I am now certified.
Thank you,
~ CoderBoss.

1 Like