Quiz

  1. Why does javascript have a unique object type for dates?

    I dunno. Good question; I just don't know the answer.

  2. This creates a variable called dateObj2
    
        var dateObj2 = new Date(); 
    
    

    , how is it different from ""Object""? I'm a bit confused by how this resembles Java but not exactly the same. Why is ""Date"" an object but not dateObj2?

    When we're invoking methods e.g. dateObj2.getFullYear() , does it mean that we call methods on the variable dateObj2?

    Object is like a Java class. In fact it is exactly like the Object class in Java: it is the ancestor of all classes. See Java Object Class

    The value of dateObj2 is an object. It's a particular date, which is different from the Object class.

    The value of Date is a class.

    Yes, we can call Date methods on dateObj2

  3. weekdays.splice(2,1,"Odin's Day"); what does the one indicate ? Odin's day replaces third element, but what does 1 mean?

    The 1 is the deleteCount: we are deleting one element. See: MDN Array Splice

  4. Can both String() and toFixed be used in turn a number into string? What's the difference between them? Thank you!

    Yes. toFixed has more features, like choosing a radix.

  5. I'm confused about question 4--aren't person1.age and person1[age] equivalent?

    Good question! No, they aren't. Because in person1[age], age is a variable, while in person1.age is it a literal.

    Consider the following code:

    
        var x = 'quick'
        var y = 'ly'
        var s1 = x+y;
        var s2 = x+'er';
    
    
  6. In what case we would not know the names of the properties? / In the Unknown Properties section of the reading, you say "The expression p1[k] means to look up a key in the dictionary in p1 where the name of the key is in the variable k", but where is the variable k? Was it ever initialized? Just not sure where the k is coming from.

    unknown properties

    Let's play with the JS code in the browser.

  7. when to use unknown properties

    When the property you need to access is in a variable instead of known in advance. Here's another example:

    
        capitals = {us: 'Washington DC', china: 'Beijing', india: 'New Delhi', france: 'Paris'};
        function cap_quiz() {
            let country = prompt("What country?");
            alert("the capital of "+country+" is "+capitals[country])
        };
    
    
  8. How would you change values in a dictionary in a loop format for eg if you had to add the string "str' to all values against their keys. Would you still use for each method and just create a new method and invoke that inside another method you create for the updated dictionary. Sorry if this is confusing, Im trying to think of how we navigate such a problem in python and if javascript handles it similarly. Thankyou!

    Suppose we wanted to add an exclamation point to all the capitals:

    
    Object.keys(capitals).forEach(cap => capitals[cap] += "!");
    
    
  9. Just wanna say hi and thanks

    Hi!