Quiz

  1. About question 1--we're technically duplicating the template, and not the clone, right?

    Correct. The clone is the copy, and the template is the original.

  2. The reading cover scenarios of using a single criteria (name, age, class year) but what about multi properties that could be used to sort (for example, name followed by class year), how could .sort be used then?

    What a great question! Let's consider classyear, then name. So, "Alice class of 2025" goes before "Alice class of 2026".

    Consider ties on the classyear. Then, and only then, do we look at the name.

    
        function cmp_classyear_then_name(a, b) {
            if( a.classyear == b.classyear ) {
                return a.name.localeCompare(b.name);
            } else {
                return a.classyear.localeCompare(b.classyear);
            }
        }
    
        // test data
    
        let studs = [
            {id: 0, name: "Alice", classyear: "2025"},
            {id: 1, name: "Alice", classyear: "2026"},
            {id: 2, name: "Betty", classyear: "2025"},
            {id: 3, name: "Betty", classyear: "2026"}
        ];
    
        // in action
        studs.sort(cmp_classyear_then_name);
        console.table(studs);