samedi 19 juin 2021

if else statement javascript returns false statement

Could someone help me with this code, I'm trying to write a function that takes in a name from an object and returns a name tag : "Hi! I'm [name], and I'm from [country]."

I've tried this code

const GUEST_LIST = {
    Randy: "Germany",
    Karla: "France",
    Wendy: "Japan",
    Norman: "England",
    Sam: "Argentina"
}

function greeting(name) {
  var x = Object.keys(GUEST_LIST);
  const array = Object.keys(GUEST_LIST)
    .map(function(key) {
        return GUEST_LIST[key];
    });
  
  
  var txt ="";
  for (let i in x)
    {
      if (name === x[i])
        {
          txt = "Hi I'm "+x[i] +", and I'm from " +array[i];  
        }
      else
        {
          txt = "Hi! I'm a guest";
        }
      
    }
     return txt;
}
console.log(greeting("Randy"))

but it always returns "Hi! I'm a guest" except when I type Sam,

Aucun commentaire:

Enregistrer un commentaire