0

I created a function that uses the method Object.keys(myObj) to generate a random key from its array. I want to use the value returned by the variable currentCountry to grab the corresponding value from the matching key/value pair in my object.

I tried directly substituting the variable name in to try and call the method that way (countryObj.currentCountry), but that came back 'undefined'

I've done a good bit of Google searching but so far haven't found what I need to do this.

let countryObj = {
    Argentina: 'buenos Aires',
    Bolivia: 'Sucre',
    Brazil: 'Brasilia',
    Chile: 'Santiago',
    Columbia: 'Bogotá',
    Ecuador: 'Quito',
    Guyana: 'Georgetown',
    Paraguay: 'Asunción',
    Peru: 'Lima',
    Suriname: 'Paramaribo',
    Uruguay: 'Montevideo',
    Venezuela: 'Caracas'
} 

let keys = Object.keys(countryObj); // returns an array of the keys (country names) in countryObj

let currentCountry = "";
let randomCountry =  function() { // returns a randomly chosen country from the keys array and writes
    // ... the country to the <p> element in index.html    
    currentCountry = keys[Math.floor(keys.length * Math.random())];
    document.getElementById('p').innerHTML = currentCountry;
    return currentCountry;

When I tried directly subbing the variable name to call the method it returned 'undefined'

temeryunc
  • 11
  • 1
  • You might don't call randomCountry function and also If you want to show it you should add to the p element an id or class or use getElementsByTagName – mssoheil Aug 10 '19 at 04:49
  • Your code works fine as is. Show us how you called the function. – dwmorrin Aug 10 '19 at 04:52

1 Answers1

0

I tried directly substituting the variable name in to try and call the method that way (countryObj.currentCountry), but that came back 'undefined'

Rather than countryObj.currentCountry, you should use countryObj[currentCountry], the syntax is the same as an array, but if countryObj is an object, then it returns the value for the key currentCountry on the object.

You don't show how you call the function, but a working example could be:

let countryObj = {
    Argentina: 'buenos Aires',
    Bolivia: 'Sucre',
    Brazil: 'Brasilia',
    Chile: 'Santiago',
    Columbia: 'Bogotá',
    Ecuador: 'Quito',
    Guyana: 'Georgetown',
    Paraguay: 'Asunción',
    Peru: 'Lima',
    Suriname: 'Paramaribo',
    Uruguay: 'Montevideo',
    Venezuela: 'Caracas'
} 

let keys = Object.keys(countryObj); // returns an array of the keys (country names) in countryObj


let randomCountry =  function() {
    // returns a randomly chosen country from the keys array and writes
    // ... the country to the <p> element in index.html    
    let currentCountry = keys[Math.floor(keys.length * Math.random())];
    document.getElementById('p').innerHTML = currentCountry;
    return currentCountry;
}

let currentCountry = randomCountry();

console.log(countryObj[currentCountry]);
zakum1
  • 895
  • 6
  • 20
  • To test this I used console.log(countryObj[currentCountry]); and in the chrome browser it did not have any effect. Nothing was logged in the JS console. I do not think it is accepting the variable in place of the object method. – temeryunc Aug 10 '19 at 07:45
  • I edited to show a fuller example - I changed currentCountry to be local to randomCountry, but you can easily make it global again if that is needed – zakum1 Aug 10 '19 at 07:53
  • 1
    If you get no console output then randomCountry() is not being called. You should include more code to show the flow of execution. – zakum1 Aug 10 '19 at 08:08