Lesson 2
Traversing Digits and Summing Even Numbers
Introduction

Welcome! In today's lesson, we'll explore an intriguing coding challenge that involves traversing the digits of a number using a while loop under specific conditions in Ruby.

This is a great opportunity to practice and enhance your skills in working with Ruby's loops and conditional statements, which are fundamental concepts in programming. Are you ready to dive in? Let's get started!

Task Statement

Today's objective is to create a method that operates on an integer input. The task might seem straightforward, but it demands some ingenuity. Here's the mission: given an integer n, we need to calculate and return the sum of its even digits — and here's the catch — without converting n into a string. For example, if n equals 4625, the output should be 12 because the sum of the even digits 4, 6, and 2 equals 12.

Keep in mind that n will always be a positive integer between 1 and 100_000_000. Ready to give it a try? Excellent! Let's move on!

Solution Building: Step 1

To start, we need the basic structure of our method, where we define a variable digit_sum to keep track of the sum of even digits.

Below is the initial framework for our method:

Ruby
1def sum_of_even_digits(n) 2 digit_sum = 0 3 # Our code will evolve from here 4end

This framework establishes a foundation on which we'll incrementally build our solution.

Step 2: Setting up the Loop

The tool we've chosen to traverse through the digits of the input integer n is the while loop, which is set to run as long as n is greater than zero. Let's incorporate this into our method:

Ruby
1def sum_of_even_digits(n) 2 digit_sum = 0 3 while n > 0 4 # We'll develop our method from here 5 end 6end

By adding a while loop, we're enabling the method to process each digit of the number one by one.

Step 3: Extracting and Processing Each Digit

Inside our loop, we'll first extract the last digit of n using the modulo operation (n % 10). If the digit is even, we'll increase our digit_sum by this digit.

After processing a digit, we'll chop off the last digit of n using integer division (n / 10). Here's how this appears in the method:

Ruby
1def sum_of_even_digits(n) 2 digit_sum = 0 3 while n > 0 4 digit = n % 10 5 if digit.even? # Check if the digit is even 6 digit_sum += digit 7 end 8 n = n / 10 # Remove the last digit 9 end 10end

This loop processes each digit appropriately, checking for even numbers and updating the sum as needed.

Step 4: Returning the Result

After summing up all the even digits, the final step is to ensure our method returns the digit_sum. In Ruby, the last evaluated expression in a method is implicitly returned, so we simply ensure digit_sum is the last line:

Ruby
1def sum_of_even_digits(n) 2 digit_sum = 0 3 while n > 0 4 digit = n % 10 5 if digit.even? # Check if the digit is even 6 digit_sum += digit 7 end 8 n = n / 10 # Remove the last digit 9 end 10 digit_sum 11end 12 13# Example usage: 14result = sum_of_even_digits(4625) 15puts result # Output: 12

Including digit_sum as the last line ensures that the calculated sum for even digits is returned as the final output of the method.

Lesson Summary

Well done on completing this lesson! You've successfully navigated the foundational concepts of using a while loop to traverse the digits of a number in Ruby and have gained an understanding of how to apply conditions within it.

Now, it's your turn to apply what you've learned. I invite you to explore additional challenges to solidify and build upon your new skill set. Remember, the only limit to your growth is the boundary of your dedication. Keep practicing; your Ruby prowess is growing with each challenge you conquer!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.