educative.io

https://www.educative.io/module/page/8q5JgjuBXZGgkWpxl/10370001/4736191808405504/5623565505527808

Quick question. Why result is initialized with -99 instead of 0?


Course: https://www.educative.io/collection/10370001/5658174447157248
Lesson: https://www.educative.io/collection/page/10370001/5658174447157248/6275629179731968

The initial assignment of -99 to the result variable appears to be an arbitrary placeholder value chosen by the code author. This value has no specific meaning in the context of the code, and it doesn’t affect the final result of the function when determining whether a number is even or odd.

def even_or_odd(num)
  result = -99  # This is an arbitrary placeholder value
  result = num % 2  # This line calculates the actual result
  return result
end

The meaningful calculation occurs on the line where result is assigned the result of the modulo operation (num % 2). The -99 value is effectively replaced by the result of the modulo operation when the function is called with a specific num value.

So, the use of -99 in this context is arbitrary and doesn’t serve any functional purpose. It could have been initialized to 0, nil, or any other value without affecting the core logic of the function.

I hope this helps!

1 Like

Thanks a lot for your time.

Cheers!


Course: https://www.educative.io/collection/10370001/5658174447157248
Lesson: https://www.educative.io/collection/page/10370001/5658174447157248/6275629179731968

2 Likes