Try   HackMD

CS1101S Studio S2 (T08C)

Source Academy Burger Joint

Suppose we're designing a point-of-sale and order-tracking system for a new burger joint. It is a small joint and it only sells 4 options for combos: Classic Single Combo (hamburger with one patty), Classic Double With Cheese Combo (2 patties), and Classic Triple with Cheese Combo (3 patties), Avant-Garde Quadruple with Guacamole Combo (4 patties). We shall encode these combos as 1, 2, 3, and 4 respectively. Each meal can be biggie-sized to acquire a larger box of fries and drink. A biggie-sized combo is represented by 5, 6, 7, and 8 respectively, for combos 1, 2, 3, and 4 respectively.

Question 1

Write a function named biggie_size which when given a regular combo returns a biggie-sized version.

function biggie_size(combo) {
    return combo + 4;
}

Question 2

Write a function named unbiggie_size which when given a biggie-sized combo returns a non-biggie-sized version.

function unbiggie_size(biggie) {
    return biggie - 4;
}

Question 3

Write a function named is_biggie_size which when given a combo, returns true if the combo has been biggie-sized and false otherwise.

// Best solutions, concise and works
function is_biggie_size(combo) {
    return combo >= 5;
}

function is_biggie_size(combo) {
    return combo > 4;
}

// Also good except the studio sheet specifies that only valid inputs are allowed, so slightly more work
function is_biggie_size(combo) {
	return (5 <= combo) && (combo <= 8);
}

// Extra work since the predicate already returns the correct booleans
function is_biggie_size(burgercombo) {
    return burgercombo >= 5 ? true : false;
}

// Would be good if the studio sheet didn't already specify that only valid inputs are allowed (ensures that the function only works on the 4 integers specified)
function is_biggie_size(combo) {
    return combo === 5
        ? true
        : combo === 6
            ? true
            : combo === 7
                ? true
                : combo === 8
                    ? true
                    : false;
}

Question 4

Write a function named combo_price which takes a combo and returns the price of the combo. Each patty costs $1.17, and a biggie-sized version costs $0.50 extra overall.

// Best solution, easily readable and works
function combo_price(combo_number) {
    return is_biggie_size(combo_number) 
        ? (unbiggie_size(combo_number) * 1.17) + 0.50
        : combo_number * 1.17;
}

// Not as good, while it's functionally equivalent to the above solution and shorter, repeats the predicate which is undesirable as the desired predicate might change, also less readable
function combo_price(combo){
    return x > 4
        ? (combo-4) * 1.17 + 0.50
        : 1.17 * combo;
} 

Question 5

An order is a collection of combos. We will encode an order as each digit representing a combo. For example, the order 237 represents a Double, Triple, and biggie-sized Triple.

Write a function named empty_order which takes no arguments and returns an empty order which is represented by 0.

function empty_order() {
    return 0;
}

Question 6

Write a function named add_to_order which takes an order and a combo and returns a new order which contains the contents of the old order and the new combo. For example, add_to_order(1, 2) returns 12.

function add_to_order(order, combo) {
    return (order * 10) + combo;
}

Question 7

Write a function named last_combo which takes an order and returns the last combo. For example, last_combo(321) returns 1.

// This question teaches the importance of Googling! :)
function last_combo(order) {
    return order % 10;
}

Question 8

Write a function named other_combos which takes an order and returns a new order without the last combo. For example, other_combos(321) returns 32.

// Both solutions are equally good, the first solution uses a function that wasn't previously taught in lectures but is more concise, the second solution relies on the last_combo function to work, which may or may not break depending on whether the function changes
function other_combos(order){
    return math_floor(order / 10);
}

function other_combos(order){
    return (order - last_combo(order)) / 10;
}