Hi!
I just finished my Mixed Messages portfolio project and would love to have some feedback if anyone has any to offer.
Thanks!
Ben.
Hi!
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!
Hi,
Thanks for taking time to look over it. 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.
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.
Thank you so much!
I was on the right lines but had to peek in the end!
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?
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.
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.