CodeWars: Simple Multiplication

Ross Andrews
4 min readJun 13, 2024

--

The Goal of the Challenge

For this kata, we need to take a random integer and determine if it is even, and then multiply that number by 8. If the number isn’t even, we will take the product of that number and 9.

NOTE: this write-up will cover using if statements and creating a function. I might post another covering this challenge using match expressions and bit shifting in the future.

Making Sense of the Function Signature

// the provided function signature
fn simple_multiplication(number: u8) -> u8 {
todo!
}

Looking at the function we are provided, we are able to determine the following:

  1. The function takes one parameter in the form of an unsigned 8-bit integer.
  2. The function is going to return an unsigned 8-bit integer.

We know we are working with unsigned 8-bit integers, because u8 is a primitive representing that value. We also know that the function only takes one argument called number.

Parameters for functions are stored in parentheses after the function name, followed by a “:” and data type.

Determining if the Variable is Even or Odd

To try and make this simple code as clean as we can, I’m going to create a separate function to test if the number is even. This isn’t necessary and this step can definitely be implemented in simple_multiplication(), but why not.

First thing we need to do is have our second function return a boolean value, true or false. To do that we can use the -> operator and the bool primitive.

-> bool { }

We will call the function is_even and have it take one parameter (n) with a u8 primitive so we don’t run into issues of conflicting data types.

Second function signature. Note: fn is the keyword for making functions.

Evaluating if a number is even is fairly simple to do. We can take the variable n and compute the modulo 2 and set it equal to zero. What the modulo does is return any remainder after division. This means that an even number will return zero, but an odd one will not, giving us a boolean result. Here’s how the is_even() function should look:

Function to evaluate if a number is even.

NOTE: we don’t need a closing “;” in our function because it is an expression, not a statement.

Creating the if Statement to get the Answer

To use if statements, you must have a condition that can evaluate to a boolean answer or else it will not work. In other words, if something is true, do this action; or else, do this other thing. Since our new function takes care of that for us, we can simply call is_even() after the if keyword. If the condition is true, we will multiply by 8, if not, 9. Your code should look something similar to the following screenshot:

The completed if statement.

Testing our Code

Lets make sure this works by making a main() function. To do this, we will create two variables that will supply an even and an odd number to simple_multiplication(). To declare a variable in Rust, we use the let keyword.

As a side note, by default, variables in Rust are immutable (cannot change) when they don’t have the mut key word before the variable name.

fn main() {
let first_number = simple_multiplication(8);
let second_number = simple_multiplication(9);
}

We can then use the println! macro to return the results to standard out.

NOTE: we use “{variable_name}” to call variables in println!.

Your main() should look like the screenshot below:

The main() function is made up of statements, which is why we terminate each line with “;”
Running the code gives us the result for 8 * 8 and 9 * 9, it works!

The Complete Code

If you would like to learn more about Rust, I highly suggest you checkout the following resources:

  1. The Rust Book
  2. Rustlings
  3. Rust by Example

--

--