Skip to content

Commit 4e38d84

Browse files
committed
Adding 3rd exercise
1 parent 2be47a5 commit 4e38d84

File tree

2 files changed

+245
-0
lines changed

2 files changed

+245
-0
lines changed

02_calculator/calculator_spec.rb

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# # Topics
2+
#
3+
# * functions
4+
# * math
5+
# * arrays
6+
# * iterating/looping
7+
#
8+
# # Calculator
9+
#
10+
# you will build a simple calculator script with the following methods:
11+
#
12+
# `add` takes two parameters and adds them
13+
#
14+
# `subtract` takes two parameters and subtracts the second from the first
15+
#
16+
# `sum` takes an *array* of parameters and adds them all together
17+
#
18+
# # Warning
19+
#
20+
# You may not have enough knowledge yet to complete `sum`. You will probably
21+
# need to use **loops** (e.g. `while`) or **iterators** (e.g. `each`) to
22+
# get the tests to pass.
23+
#
24+
# # Bonus
25+
#
26+
# There are also some bonus exercises for when you finish the regular ones. The bonus round will help teach you test-driven *development*, not simply test-guided *learning*.
27+
#
28+
# Your mission, should you choose to accept it, is to write *tests* for three new methods:
29+
#
30+
# * `multiply` which multiplies two numbers together
31+
# * `power` which raises one number to the power of another number
32+
# * `[factorial](http://en.wikipedia.org/wiki/Factorial)` (check Wikipedia if you forgot your high school math).
33+
#
34+
#
35+
36+
require "calculator"
37+
38+
describe "add" do
39+
it "adds 0 and 0" do
40+
add(0,0).should == 0
41+
end
42+
43+
it "adds 2 and 2" do
44+
add(2,2).should == 4
45+
end
46+
47+
it "adds positive numbers" do
48+
add(2,6).should == 8
49+
end
50+
end
51+
52+
describe "subtract" do
53+
it "subtracts numbers" do
54+
subtract(10,4).should == 6
55+
end
56+
end
57+
58+
describe "sum" do
59+
it "computes the sum of an empty array" do
60+
sum([]).should == 0
61+
end
62+
63+
it "computes the sum of an array of one number" do
64+
sum([7]).should == 7
65+
end
66+
67+
it "computes the sum of an array of two numbers" do
68+
sum([7,11]).should == 18
69+
end
70+
71+
it "computes the sum of an array of many numbers" do
72+
sum([1,3,5,7,9]).should == 25
73+
end
74+
end
75+
76+
# Extra Credit Test-Driving Bonus:
77+
# once the above tests pass,
78+
# write tests and code for the following:
79+
80+
describe "#multiply" do
81+
82+
it "multiplies two numbers"
83+
84+
it "multiplies several numbers"
85+
86+
end
87+
88+
describe "#power" do
89+
it "raises one number to the power of another number"
90+
end
91+
92+
# http://en.wikipedia.org/wiki/Factorial
93+
describe "#factorial" do
94+
it "computes the factorial of 0"
95+
it "computes the factorial of 1"
96+
it "computes the factorial of 2"
97+
it "computes the factorial of 5"
98+
it "computes the factorial of 10"
99+
end

02_calculator/index.html

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<html>
2+
<head>
3+
<title>Test-First Teaching: learn_ruby: calculator</title>
4+
<link href="../assets/style.css" media="screen" rel="stylesheet" type="text/css" />
5+
</head>
6+
<body>
7+
<div class="header">
8+
<h1><a href="http://testfirst.org">TestFirst.org</a></h1>
9+
<h2>the home of test-first teaching</h2>
10+
</div>
11+
<div class="nav">
12+
<h2><a href="../index.html">learn_ruby</a></h2>
13+
<b>Labs:</b>
14+
<ul>
15+
<li><a href="../00_hello/index.html">00 Hello</a></li>
16+
<li><a href="../01_temperature/index.html">01 Temperature</a></li>
17+
<li>02 Calculator</li>
18+
<li><a href="../03_simon_says/index.html">03 Simon Says</a></li>
19+
<li><a href="../04_pig_latin/index.html">04 Pig Latin</a></li>
20+
<li><a href="../05_silly_blocks/index.html">05 Silly Blocks</a></li>
21+
<li><a href="../06_performance_monitor/index.html">06 Performance Monitor</a></li>
22+
<li><a href="../07_hello_friend/index.html">07 Hello Friend</a></li>
23+
<li><a href="../08_book_titles/index.html">08 Book Titles</a></li>
24+
<li><a href="../09_timer/index.html">09 Timer</a></li>
25+
<li><a href="../10_temperature_object/index.html">10 Temperature Object</a></li>
26+
<li><a href="../11_dictionary/index.html">11 Dictionary</a></li>
27+
<li><a href="../12_rpn_calculator/index.html">12 Rpn Calculator</a></li>
28+
<li><a href="../13_xml_document/index.html">13 Xml Document</a></li>
29+
<li><a href="../14_array_extensions/index.html">14 Array Extensions</a></li>
30+
<li><a href="../15_in_words/index.html">15 In Words</a></li>
31+
</ul>
32+
</div>
33+
<h1>calculator</h1>
34+
<div class="content"><div class="rspec_file"> <div class="intro"><h1>Topics</h1>
35+
36+
<ul>
37+
<li>functions</li>
38+
<li>math</li>
39+
<li>arrays</li>
40+
<li>iterating/looping</li>
41+
</ul>
42+
43+
44+
<h1>Calculator</h1>
45+
46+
<p>you will build a simple calculator script with the following methods:</p>
47+
48+
<p><code>add</code> takes two parameters and adds them</p>
49+
50+
<p><code>subtract</code> takes two parameters and subtracts the second from the first</p>
51+
52+
<p><code>sum</code> takes an <em>array</em> of parameters and adds them all together</p>
53+
54+
<h1>Warning</h1>
55+
56+
<p>You may not have enough knowledge yet to complete <code>sum</code>. You will probably
57+
need to use <strong>loops</strong> (e.g. <code>while</code>) or <strong>iterators</strong> (e.g. <code>each</code>) to
58+
get the tests to pass.</p>
59+
60+
<h1>Bonus</h1>
61+
62+
<p>There are also some bonus exercises for when you finish the regular ones. The bonus round will help teach you test-driven <em>development</em>, not simply test-guided <em>learning</em>.</p>
63+
64+
<p>Your mission, should you choose to accept it, is to write <em>tests</em> for three new methods:</p>
65+
66+
<ul>
67+
<li><code>multiply</code> which multiplies two numbers together</li>
68+
<li><code>power</code> which raises one number to the power of another number</li>
69+
<li><code>[factorial](http://en.wikipedia.org/wiki/Factorial)</code> (check Wikipedia if you forgot your high school math).</li>
70+
</ul>
71+
72+
</div>
73+
<div class="tests">
74+
<h1>Tests</h1>
75+
<a class="raw_file" href="calculator_spec.rb">calculator_spec.rb</a>
76+
<pre>
77+
require &quot;calculator&quot;
78+
79+
describe &quot;add&quot; do
80+
it &quot;adds 0 and 0&quot; do
81+
add(0,0).should == 0
82+
end
83+
84+
it &quot;adds 2 and 2&quot; do
85+
add(2,2).should == 4
86+
end
87+
88+
it &quot;adds positive numbers&quot; do
89+
add(2,6).should == 8
90+
end
91+
end
92+
93+
describe &quot;subtract&quot; do
94+
it &quot;subtracts numbers&quot; do
95+
subtract(10,4).should == 6
96+
end
97+
end
98+
99+
describe &quot;sum&quot; do
100+
it &quot;computes the sum of an empty array&quot; do
101+
sum([]).should == 0
102+
end
103+
104+
it &quot;computes the sum of an array of one number&quot; do
105+
sum([7]).should == 7
106+
end
107+
108+
it &quot;computes the sum of an array of two numbers&quot; do
109+
sum([7,11]).should == 18
110+
end
111+
112+
it &quot;computes the sum of an array of many numbers&quot; do
113+
sum([1,3,5,7,9]).should == 25
114+
end
115+
end
116+
117+
# Extra Credit Test-Driving Bonus:
118+
# once the above tests pass,
119+
# write tests and code for the following:
120+
121+
describe &quot;#multiply&quot; do
122+
123+
it &quot;multiplies two numbers&quot;
124+
125+
it &quot;multiplies several numbers&quot;
126+
127+
end
128+
129+
describe &quot;#power&quot; do
130+
it &quot;raises one number to the power of another number&quot;
131+
end
132+
133+
# http://en.wikipedia.org/wiki/Factorial
134+
describe &quot;#factorial&quot; do
135+
it &quot;computes the factorial of 0&quot;
136+
it &quot;computes the factorial of 1&quot;
137+
it &quot;computes the factorial of 2&quot;
138+
it &quot;computes the factorial of 5&quot;
139+
it &quot;computes the factorial of 10&quot;
140+
end</pre>
141+
</div>
142+
</div>
143+
</div>
144+
<div class="footer"><a href="http://testfirst.org">TestFirst.org</a></div>
145+
</body>
146+
</html>

0 commit comments

Comments
 (0)