Mixed Messages - Capital cities and heads of state

Hi! :slight_smile:

I just finished my Mixed Messages portfolio project and would love to have some feedback if anyone has any to offer.

Thanks!
Ben.

Hey, @beta5952191560.

I’ve looked at your code, and have a suggestion. Consider whether your switch...case logic is necessary. Your random function is returning a number. Think about how you could utilize that number with your already defined arrays.

Edit: The design of the page is great! :slight_smile:

1 Like

Hi,

Thanks for taking time to look over it. :slight_smile: I’m sorry my response has been slow.

I get it in theory, displaying the index value of the number being returned? I think? But I can’t think quite how to write it. :sweat_smile:

1 Like

Not sure I would use an object to store the arrays, but if I did, I would take advantage of the ability for an object to also have its own methods:

//The following are arrays in which the different random values will come from
const arrays = {
    'countries' : ['England', 'Italy', 'France', 'Germany', 'Russia', 'Spain', 'The Netherlands', 'Switzerland', 'Hungary'],
    'capitals' : ['London', 'Rome', 'Paris', 'Berlin', 'Moscow', 'Madrid', 'Amsterdam', 'Zurich', 'Budapest'],
    'leaders' : ['Boris Johnson', 'Sergio Mattarella', 'Emmanuel Macron', 'Frank-Walter Steinmeier', 'Vladimir Putin', 'Pedro Sanchez', 'Mark Rutte', 'Guy Parmelin', 'Janos Ader'],

  randomSelection(arr){
    return this[arr][Math.floor(Math.random() * this[arr].length)];
  }
};

function randomMsg() { 
    return `${arrays.randomSelection('capitals')} is the capital city of ${arrays.randomSelection('countries')}. The head of state there is ${arrays.randomSelection('leaders')}!`
};

console.log(randomMsg());

Edited 6/14/21 to correct mistake in getting the array length.

1 Like

Thank you so much!

I was on the right lines but had to peek in the end! :zipper_mouth_face:

I did something similar but it was printing the index number rather than the actual value itself, hence why I ended up going down the switch route. I still don’t quite get which bit has translated the index numbers in the code you’ve kindly sent me though? :thinking:

Just realized my code had a mistake in it. I’ve copied it (with the mistake fixed) into a codebyte, and added a console.log() statement and some comments. Hopefully this helps explain. If you still have questions, feel free to ask.

const arrays = { 'countries' : ['England', 'Italy', 'France', 'Germany', 'Russia', 'Spain', 'The Netherlands', 'Switzerland', 'Hungary'], 'capitals' : ['London', 'Rome', 'Paris', 'Berlin', 'Moscow', 'Madrid', 'Amsterdam', 'Zurich', 'Budapest'], 'leaders' : ['Boris Johnson', 'Sergio Mattarella', 'Emmanuel Macron', 'Frank-Walter Steinmeier', 'Vladimir Putin', 'Pedro Sanchez', 'Mark Rutte', 'Guy Parmelin', 'Janos Ader'], randomSelection(arr){ console.log(`randomSelection called with '${arr}' passed.\fThe length of the array is ${this[arr].length}`) return this[arr][Math.floor(Math.random() * this[arr].length)]; //this refers to the emcompassing object which is arrays //[arr] is the name of the property we are accessing (key). The value returned is the array. //[Math.floor(Math.random() * this[arr].length)] This is the index. The random number will be generated, and used as the index. } }; function randomMsg() { return `${arrays.randomSelection('capitals')} is the capital city of ${arrays.randomSelection('countries')}. The head of state there is ${arrays.randomSelection('leaders')}!` }; console.log(randomMsg());

Note: You can click on the left or right Arrow Chevron icons to collapse/expand the code or output areas for easier viewing of each.
image

1 Like