Quiz

  1. For question 2, the reading said that we don't use ID. Does this mean "usually not use" or "we can't use"?

    I appreciate your careful reading! The next part says:

    While it's certainly possible to create IDs for dynamic elements and use them successfully, it's tricky and usually unnecessary. I suggest avoiding ID on all dynamic elements.

    Which I meant as "usually not have IDs"

  2. what does .find() return?

    Like pretty much all jQuery methods, it returns a "wrapped set" of the items it matches. We used find in CoffeeRun to find the desired checkbox, and we used find in the Quizzes assignment to find the user's answer.

  3. can you explain how the subtraction in the number sort works?

    Sure. Let's take an example like this:

    
    [5,7].sort((a,b) => a-b)
    
    
    The function gets invoked on 5 and 7. Since 5-7 is -2, which is negative, the function is saying that 5 should go before 7.
  4. Could you explain why strArray.sort(cmp) works when cmp taking parameters a and b. Are a and b every possible pair of strings in strArray that get passed into cmp and compared?

    Well, not *every* pair (that's an n2 algorithm), but *any* pair. Let's do a very verbose example:

    
        function verboseSubtract(a,b) {
            let diff = a-b;
            console.log('compare',a,b,'returns',diff);    
            return diff;
        }
    
    

    Let's try it on this 8 element array:

    
        [1, 11, 2, 12, 3, 13, 4, 14].sort(verboseSubtract);
    
    

    That yields 13 comparisons, not 64 and better than n log n = 3*8 = 24.