Skip to content

Studio 2

Link to Slides

Studio Worksheets

  1. Studio 2 Worksheet

x_combo

Implement a function to get a specific order from the combo. Let’s call this function x_combo where x refers to the position of a order from the back of a combo.

  1. x is one-indexed
  2. Assume that all inputs are valid - x will never be more than the number of digits in combo
function x_order(order, x) {
    // YOUR SOLUTION HERE 
}

// Examples
x_order(1234, 1) // 4
x_order(1234, 3) // 2
x_order(12345678, 8) // 1
Answers
  1. Recognize that everytime we do a / 10, we shift the order to the right by 1, therefore, to get to the x position, we can do order / 10^(x-1)
  2. Make use of the already implemented last_combo
function last_combo(order) {
    // Question 7 in Studio 2 Worksheet
    return order % 10;
}

function x_order(order, x) {
    return last_combo(math_floor(order / math_pow(10, x-1)));
}