Advertisement
JPDG13

Maps, different means, ES6

Apr 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ES6 Maps
  2.  
  3. const question = new Map();
  4. // Key value pair is here.
  5. question.set('question', 'What is the name of the latest JS version?')
  6. question.set(1, 'ES5');
  7. question.set(2, 'ES6');
  8. question.set(3, 'ES62015');
  9. question.set(4, 'ES7');
  10. question.set('correct', 3);
  11. question.set(true, 'Correct answer');
  12. question.set(false, 'Please try again');
  13. // Get is different than set.  It's "gets" the value from the const
  14. question.get('question');
  15.  
  16.  
  17.  
  18. if (question.has(4)) {
  19.     question.delete(4);
  20.     console.log('Answer 4 is here.');
  21. }
  22. //clears the entire question.
  23. //question.clear();
  24.  
  25. console.log(question.get('question'));
  26. console.log(question.size);
  27.  
  28. // This uses a ForEach loop to print the value/key pair to the console.  
  29. question.forEach((value, key) => console.log(`This is ${key} and it's set to ${value}.`));
  30.  
  31. // This is another way to do the same thing.
  32. for(let [key, value] of question.entries()) {
  33.     console.log(`This is ${key} and it's set to ${value}.`);
  34. }
  35.  
  36. for(let [key, value] of question.entries()) {
  37.     if(typeof(key) === 'number') {
  38.         console.log(`Answer ${key}: ${value}`);
  39.     }
  40. }
  41.  
  42. const ans = parseInt(prompt('Write the correct answer:'));
  43.  
  44. console.log(question.get (ans === question.get('correct')));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement