Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ES6 Maps
- const question = new Map();
- // Key value pair is here.
- question.set('question', 'What is the name of the latest JS version?')
- question.set(1, 'ES5');
- question.set(2, 'ES6');
- question.set(3, 'ES62015');
- question.set(4, 'ES7');
- question.set('correct', 3);
- question.set(true, 'Correct answer');
- question.set(false, 'Please try again');
- // Get is different than set. It's "gets" the value from the const
- question.get('question');
- if (question.has(4)) {
- question.delete(4);
- console.log('Answer 4 is here.');
- }
- //clears the entire question.
- //question.clear();
- console.log(question.get('question'));
- console.log(question.size);
- // This uses a ForEach loop to print the value/key pair to the console.
- question.forEach((value, key) => console.log(`This is ${key} and it's set to ${value}.`));
- // This is another way to do the same thing.
- for(let [key, value] of question.entries()) {
- console.log(`This is ${key} and it's set to ${value}.`);
- }
- for(let [key, value] of question.entries()) {
- if(typeof(key) === 'number') {
- console.log(`Answer ${key}: ${value}`);
- }
- }
- const ans = parseInt(prompt('Write the correct answer:'));
- console.log(question.get (ans === question.get('correct')));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement