Jump to content

Console is showing "undefined" and I don't know what's causing it

 

function fastestCoaster(country) {
  var allCoasters = getColumn("Rollercoasters","Rollercoaster Name");
  var countries = getColumn("Rollercoasters","Country");
  var parkNames = getColumn("Rollercoasters","Amusement Park");
  var coasterSpeeds = getColumn("Rollercoasters","Speed");
  var filteredCoasters = [];
  var temp = coasterSpeeds[0];
  var temp2 = allCoasters[0];
  var temp3 = countries[0];
  var temp4 = parkNames[0];
  var topSpeed;
  var topspeedcoasterName;
  var topspeedcoasterCountry;
  var topspeedcoasterparkName;
  for (var i = 0; i < allCoasters.length; i++) {
    if (countries[i] == country) {
      appendItem(filteredCoasters, allCoasters[i]);
    }
  }
  for (var k = 0; k < coasterSpeeds.length; k++) {
  if (coasterSpeeds[k] > temp && countries[k] == country) {
    temp = coasterSpeeds[k];
    temp2 = allCoasters[k];
    temp3 = countries[k];
    temp4 = parkNames[k];
  }
}
  
  topSpeed = temp;
  topspeedcoasterName = temp2;
  topspeedcoasterCountry = temp3;
  topspeedcoasterparkName = temp4;
  console.log(topSpeed);
  console.log(topspeedcoasterName);
  console.log(topspeedcoasterCountry);
  console.log(topspeedcoasterparkName);
  console.log("The fastest rollercoaster in  " + topspeedcoasterCountry + " is the " + topspeedcoasterName + " at " + topspeedcoasterparkName + "." + " Clocking in at a blazing fast " + topSpeed + " km/h" + " or about " + Math.round(topSpeed/1.609) + " mp/h");
}

console.log(fastestCoaster("Argentina"));
console.log(fastestCoaster("USA"));
console.log(fastestCoaster("Germany"));

Hey y'all. I'm working on a project for my highschool comp sci class. I'm using Code.org JS. The project is to make a library that uses parameters, returns, for loops, if statements, and of course I have to have comments explaining the code but I can do that at the very end. I'm having an issue where my console is saying "undefined" but I don't know what's returning "undefined." I just found out that the undefined statements are coming when I call the functions with a parameter at thev very end of my program. I don't know why it's giving undefined and then also what it's supposed to console.log. Can someone explain what's going on? Also, I don't know what to return. If someone could give me some tips on what would be best to return in this situation that would be great too
 

 

Edited by ReaperWolf404
Clarification
Link to comment
Share on other sites

Link to post
Share on other sites

Usually undefined can be printed if a variable or a function is not declared or it is not initialized.

The console should print the line where the "undefined thing" is located.

 

Assuming that you have the dataset "Rollercoasters" already imported, are you passing the "country" variable to the function?

For debugging purposes, can you print the console.log(country) and the console.log(Rollercoasters) ?

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/21/2023 at 7:04 AM, TheJudge91 said:

Usually undefined can be printed if a variable or a function is not declared or it is not initialized.

The console should print the line where the "undefined thing" is located.

 

Assuming that you have the dataset "Rollercoasters" already imported, are you passing the "country" variable to the function?

For debugging purposes, can you print the console.log(country) and the console.log(Rollercoasters) ?

I got it figured out. Thank you though 🙂

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, ReaperWolf404 said:

I got it figured out. Thank you though 🙂

No worries, but for anyone who can get the same problem, what was the problem and solution?

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, TheJudge91 said:

No worries, but for anyone who can get the same problem, what was the problem and solution?

I didn't have a return in my function which was causing it to spit out undefined

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×