Home > OS >  How Do I Integrate the Array in My Function?
How Do I Integrate the Array in My Function?

Time:09-17

I've been having some issues trying to complete my homework assignment to create a weather app for a long while. We are assigned a Javascript Data Types challenge to develop a prompt that "asks the user to "Enter a city" (example: Paris), then alert "It is currently 19°C (66°F) in Paris with a humidity of 80%". If the city doesn't exist in the object (i.e: Sydney), alert "Sorry, we don't know the weather for this city, try going to https://www.google.com/search?q=weather sydney". Since this is an alert, the link shouldn't be clickable. Please round the values in the Alert to the nearest whole number (no decimal points, e.g. 5.45 should be rounded to 5).

My code has undergone many variations over the past month and I am throwing in the towel to seek assistance. My main concern is to have the alert specify the temperature and humidity of each object, but I may have overcomplicated the code. The console describes errors pertaining to an unexpected assignment within the "if" statement.

Here is my code attached:

let weather = {
  paris: {
    temp: 19.7,
    humidity: 80,
  },
  tokyo: {
    temp: 17.3,
    humidity: 50,
  },
  lisbon: {
    temp: 30.2,
    humidity: 20,
  },
  "san francisco": {
    temp: 20.9,
    humidity: 100,
  },
  oslo: {
    temp: -5,
    humidity: 20,
  },
};

window.onload = function cityWeather() {
  let city = prompt("Enter a city.");
  if ((city = "paris")) {
    alert(
      `It is currently ${weather[0].temp}°C in ${city} with a humidity of ${weather[0].humidity}`
    );
  }
  if ((city = "tokyo")) {
    alert(
      `It is currently ${weather[1].temp}°C in ${city} with a humidity of ${weather[1].humidity}`
    );
  }
  if ((city = "lisbon")) {
    alert(
      `It is currently ${weather[2].temp}°C in ${city} with a humidity of ${weather[2].humidity}`
    );
  }
  if ((city = "san francisco")) {
    alert(
      `It is currently ${weather[3].temp}°C in ${city} with a humidity of ${weather[3].humidity}`
    );
  }
  if ((city = "oslo")) {
    alert(
      `It is currently ${weather[4].temp}°C in ${city} with a humidity of ${weather[4].humidity}`
    );
  } else {
    alert(
      "Sorry, we don't know the weather for this city, try going to https://www.google.com/search?q=weather sydney"
    );
  }
  let weather = math.ciel(weather.humidity);
};

CodePudding user response:

Your if statements should have == instead of =. For example if(city == "paris"). You're also indexing an object with numbers rather than with the name of the field. So it should be weather["paris"] instead of weather[0]. Using indexes would only work on an array, but your object is not an array.

CodePudding user response:

You can skip all of the conditional logic for each city and try accessing the city object directly, if it exists. After the prompt, city will be a string. Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects, properties of JavaScript objects can also be accessed or set using a bracket notation. So either weather.paris or weather["paris"] will work. We can use this bracket notation to access the value directly.

let weather = {
  paris: {
    temp: 19.7,
    humidity: 80,
  },
  tokyo: {
    temp: 17.3,
    humidity: 50,
  },
  lisbon: {
    temp: 30.2,
    humidity: 20,
  },
  "san francisco": {
    temp: 20.9,
    humidity: 100,
  },
  oslo: {
    temp: -5,
    humidity: 20,
  },
};

window.onload = function cityWeather() {
  let city = prompt("Enter a city.").toLowerCase();
  let cityWeather = weather[city];
  
  if (cityWeather !== undefined) {
    alert(
      `It is currently ${cityWeather.temp}°C in ${city} with a humidity of ${cityWeather.humidity}`
    );
  } else {
    alert(
      "Sorry, we don't know the weather for this city, try going to https://www.google.com/search?q=weather sydney"
    );
  }
  
  //let weather = math.ciel(weather.humidity);
};

  • Related