Studio 2
Studio Worksheets
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.
x
is one-indexed- Assume that all inputs are valid -
x
will never be more than the number of digits incombo
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
- Recognize that everytime we do a
/ 10
, we shift the order to the right by1
, therefore, to get to thex
position, we can doorder / 10^(x-1)
- 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)));
}