0

Employes map

I need help with this exercise because it gives me an error on the output. The description is as follows: Five employees of a company are stored on the map, in the program that you have been given. Their names are set as keys and their positions as values. The company is hiring one more employee. The program should take the name and position as inputs and store them in the existing map. Complete the program to perform this operation and send the list of employees to the console in the format shown in the sample output. Input example Bob Developer Output example Richard: Developer Maria: SEO Specialist Tom: Product Manager David: Accountant Sophia: HR Manager Bob: Developer. Use the entries () method to return an array Iterator [key, value] on the map for each item. https://code.sololearn.com/W8UvKymIfww1/?ref=app

3rd Jul 2021, 4:06 AM
Alex Narváez
Alex Narváez - avatar
17 Answers
+ 10
function main() { var name = readLine(); var position = readLine(); let employees = new Map([ ["Richard", "Developer"], ["Maria", "SEO Specialist"], ["Tom", "Product Manager"], ["David", "Accountant"], ["Sophia", "HR Manager"] ]); //your code heres goes employees.set(name,position) for(var i of employees.entries()){ console.log(i [0] + " : " + i [1]) } }
12th Oct 2021, 8:52 PM
Hasan Ali
Hasan Ali - avatar
+ 6
function main() { var name = readLine(); var position = readLine(); let employees = new Map([ ["Richard", "Developer"], ["Maria", "SEO Specialist"], ["Tom", "Product Manager"], ["David", "Accountant"], ["Sophia", "HR Manager"] ]); //your code heres goes employees.set(name,position) for(var i of employees.entries()){ console.log(i [0] + " : " + i [1]) } } Good Luck
25th Jan 2022, 9:08 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 1
if the task expect an iterator to be returned, just do: //your code heres goes employees.set(name,position); return employees.entries(); actually your code output (print) each key, value pairs ^^
3rd Jul 2021, 4:12 AM
visph
visph - avatar
+ 1
vishp and Ipang. Thank you very much for the answer, however doing it this way I get undefined, it still generates error in the output, and calling the function does not solve the problem either. I don't know how else to do it.
3rd Jul 2021, 2:28 PM
Alex Narváez
Alex Narváez - avatar
+ 1
I was thinking the "return an array iterator" was part of the task description ^^ in fact, you are only missing a space after the colon ;P console.log(k + ": " + v);
3rd Jul 2021, 3:26 PM
visph
visph - avatar
+ 1
Ipang this is a code coach / end of project / practice: no need to replace the method used to get input nor to call main function ;)
3rd Jul 2021, 3:27 PM
visph
visph - avatar
+ 1
you must put a space after the colon, but not before ^^
3rd Jul 2021, 3:49 PM
visph
visph - avatar
+ 1
marking your own answer as best is not allowed and doesn't give you any xp ;P
3rd Jul 2021, 4:02 PM
visph
visph - avatar
+ 1
function main() { var name = readLine(); var position = readLine(); let employees = new Map([ ["Richard", "Developer"], ["Maria", "SEO Specialist"], ["Tom", "Product Manager"], ["David", "Accountant"], ["Sophia", "HR Manager"] ]); //your code heres goes employees.set(name,position) for(var i of employees.entries()){ console.log(i [0] + " : " + i [1]) // put space before and after of : (colon) } }
26th Aug 2023, 3:14 PM
Naveen Kumar Banoth
Naveen Kumar Banoth - avatar
+ 1
//Alternative way of putting the key and value function main() { var name = readLine(); var position = readLine(); let employees = new Map([ ["Richard", "Developer"], ["Maria", "SEO Specialist"], ["Tom", "Product Manager"], ["David", "Accountant"], ["Sophia", "HR Manager"] ]); // Add the new pair to the map employees.set(name, position); // Iterate over the map and print out each employee's name and position for (var [key, value] of employees.entries()) { console.log(key + " : " + value); } }
15th Jun 2024, 7:14 AM
Richard Kuthita
Richard Kuthita - avatar
0
What error? I see no error. I see you don't call main() function so it doesn't run.
3rd Jul 2021, 4:13 AM
Ipang
0
Replace readLine() with prompt(<title>). Change <title> to your liking, for asking name and position. Then you call main(), it works for me.
3rd Jul 2021, 3:26 PM
Ipang
0
Visph, I don't get a prompt asking for name & position when running the code as it is a Web project. I have to use prompt() to get such prompt, and call main(). Of course it works without changing input method or calling main() had this were a Node code.
3rd Jul 2021, 3:32 PM
Ipang
0
visph and Ipang. Thank you very much for the suggestions and help you gave me, I have already managed to correct the code and modify it as follows:
3rd Jul 2021, 3:48 PM
Alex Narváez
Alex Narváez - avatar
3rd Jul 2021, 3:48 PM
Alex Narváez
Alex Narváez - avatar
0
function main() { var name = readLine(); var position = readLine(); let employees = new Map([ ["Richard", "Developer"], ["Maria", "SEO Specialist"], ["Tom", "Product Manager"], ["David", "Accountant"], ["Sophia", "HR Manager"] ]); //your code heres goes employees.set(name,position) for(var i of employees.entries()){ console.log(i [0] + " : " + i [1]) } }
10th Mar 2023, 6:38 PM
Krishn Kant Sharma
Krishn Kant Sharma - avatar
0
function main() { var name = readLine(); var position = readLine(); let employees = new Map([ ["Richard", "Developer"], ["Maria", "SEO Specialist"], ["Tom", "Product Manager"], ["David", "Accountant"], ["Sophia", "HR Manager"] ]); //your code heres goes employees.set(name,position) for(var i of employees.entries()){ console.log(i [0] + " : " + i [1]) } }
27th Mar 2023, 7:07 AM
Sevgi Baci
Sevgi Baci - avatar