How to resolve the algorithm Even or odd step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Even or odd step by step in the Wren programming language

Table of Contents

Problem Statement

Test whether an integer is even or odd. There is more than one way to solve this task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Even or odd step by step in the Wren programming language

Source code in the wren programming language

import "./fmt" for Fmt

var isEven1 = Fn.new { |i| i & 1 == 0 }

var isEven2 = Fn.new { |i| i % 2 == 0 }

var tests = [10, 11, 0,  57,  34,  -23,  -42]
System.print("Tests    : %(Fmt.v("s", -4, tests, 0, " ", ""))")
var res1 = tests.map { |t| isEven1.call(t) ? "even" : "odd" }.toList
System.print("Method 1 : %(Fmt.v("s", -4, res1, 0, " ", ""))")
var res2 = tests.map { |t| isEven2.call(t) ? "even" : "odd" }.toList
System.print("Method 2 : %(Fmt.v("s", -4, res2, 0, " ", ""))")

  

You may also check:How to resolve the algorithm Angles (geometric), normalization and conversion step by step in the C++ programming language
You may also check:How to resolve the algorithm Bioinformatics/Sequence mutation step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the Oforth programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Erlang programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Nanoquery programming language