1

I'm currently in a Coding class and I'm making an app for a project. We have to insert Datasets into our app. The problem I am experiencing is that when I use my dropdown menu for options in my dataset, after the code is run the first time, Code.org inserts an extra space in the design tab after my options, which causes the code to stop working. AKA: The little white space after all the options.

This is the code I have right now.

onEvent("searchMajorsButton", "click", function( ) {
setScreen("searchScreen");
});

onEvent("searchMajorButton", "click", function() {
  var salaryWanted = getText("salaryInput");
  var categoryChosen = getText("majorCategoryDropdown");
  if (salaryWanted != "Any") {
    salaryWanted = Number(salaryWanted);
  }
  readRecords("College Majors & Incomes", {}, function(records) {
    var filtered = records.filter(function(item) {
      var matchesSalary = true;
      var matchesCategory = true;
      if (salaryWanted != "Any") {
        matchesSalary = (item.Median >= salaryWanted);
      }
      if (categoryChosen != "Any") {
        matchesCategory = (item.Major_category == categoryChosen);
      }
      return matchesSalary && matchesCategory;
    });
    if (filtered.length > 0) {
      var major = filtered[0];
      var employmentRate = Math.round((major.Employed / major.Total) * 100);
      setText("resultMajorName", major.Major);
      setText("resultMajorCategory", major.Major_category);
      setText("resultMedianSalary", "$" + major.Median);
      setText("resultEmploymentRate", employmentRate + "%");
    } else {
      setText("resultMajorName", "No matching majors found");
      setText("resultMajorCategory", "-");
      setText("resultMedianSalary", "-");
      setText("resultEmploymentRate", "-");
    }
    setScreen("resultsScreen");
  });
});
onEvent("backButton1", "click", function( ) {
  setScreen("homeScreen");
});
onEvent("backButton3", "click", function( ) {
  setScreen("homeScreen");
});
onEvent("backButton2", "click", function() {
  setScreen("searchScreen");
  setProperty("salaryInput", "text", "Any");
  setProperty("majorCategoryDropdown", "text", "Any");
});
onEvent("homeButton", "click", function( ) {
  setScreen("homeScreen");
});
onEvent("viewMajorsButton", "click", function( ) {
  setScreen("allMajorsScreen");
    setText("allMajorsTextArea", "Loading majors...");
  readRecords("College Majors & Incomes", {}, function(records) {
    var majorsText = "";
    for (var i = 0; i < records.length; i++) {
      majorsText += (i + 1) + ". " + records[i].Major + " (" + records[i].Major_category + ")\n";
    }
    setText("allMajorsTextArea", majorsText);
  });
});
onEvent("filterByCategoryButton", "click", function( ) {
  setScreen("searchScreen");
});
onEvent("salaryInput", "change", function( ) {
    console.log("Selected option: " + getText("salaryInput"));
});

1 Answer 1

2

Have you tried?

var categoryChosen = getText("majorCategoryDropdown").trim();

To ensure your comparison logic is robust, you must trim any leading or trailing whitespace from the value you retrieve from the dropdown before using it to filter your records.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I did, still didn't work. I just redid the project using something else. Thank you so much for the help though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.