-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathactivity-functions.html
163 lines (128 loc) · 5.94 KB
/
activity-functions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html>
<head>
<title>AC Workshop - Activity: Functions</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<div class="container">
<div class="container-inner">
<div class="left-sidebar">
<div class="home-anchor">
<h3><a href="index.html">Intro to Programming with JavaScript</a></h3>
</div>
<div class="navigation-links">
<div class="navigation-link-item"><a href="activity-logging.html">Activity: Logging</a></div>
<div class="navigation-link-item"><a href="lecture-variables.html">Lecture: Variables</a></div>
<div class="navigation-link-item"><a href="activity-variables.html">Activity: Variables</a></div>
<div class="navigation-link-item"><a href="lecture-arrays.html">Lecture: Arrays</a></div>
<div class="navigation-link-item"><a href="activity-arrays.html">Activity: Arrays</a></div>
<div class="navigation-link-item"><a href="lecture-conditionals.html">Lecture: Conditionals</a></div>
<div class="navigation-link-item"><a href="activity-conditionals.html">Activity: Conditionals</a></div>
<div class="navigation-link-item"><a href="assignment-webproposal.html">Assignment: Web App Proposal</a></div>
<div class="navigation-link-item"><a href="lecture-loops.html">Lecture: Loops</a></div>
<div class="navigation-link-item"><a href="activity-arraysadvanced.html">Activity: Arrays (Advanced)</a></div>
<div class="navigation-link-item"><a href="lecture-functions.html">Lecture: Functions</a></div>
<div class="navigation-link-item"><strong><em><a href="activity-functions.html">Activity: Functions</a></em></strong></div>
<div class="navigation-link-item"><a href="activity-fizzbuzz.html">Activity: Fizz Buzz</a></div>
<div class="navigation-link-item"><a href="teamchallenge.html">Team Challenge</a></div>
</div>
</div>
<div class="content">
<h1>Activity: Functions</h1>
<h2>What's wrong with this code? List out every problem:</h2>
<pre><code>const 3TimesArgument = func(argument) {
arg * 3 return
}</code></pre>
<h2>Finish this function:</h2>
<p>
A <b>perfect square</b> is defined as a number that has a square-root that is a <b>whole</b> number. For example:
<ul>
<li><b>9</b> has a square root of <b>3</b>, which is a whole number. It is a perfect square.</li>
<li><b>16</b> has a square root of <b>4</b>, which is a whole number. It is a perfect square.</li>
<li><b>2</b> has a square root of approximately <b>1.414</b>, which is <b>NOT</b> a whole number. It is <b>NOT</b> a perfect square.</li>
</ul>
</p>
<p>
I set up the conditional part of the <code>isPerfectSquare</code> function. It works because if the square-root of a number and the <i>rounded down value</i> of that square-root is the same then it's a whole number, and thus the input number is a perfect square.
</p>
<pre><code>const isPerfectSquare = function(arg) {
// Some code belongs here.
// Hint: what variable needs to be created and assigned?
if(Math.floor(sqrtOfArg) == sqrtOfArg) {
// It's a perfect square!
} else {
// It's not
}
}
isPerfectSquare(9); // returns true
isPerfectSquare(16); // returns true
isPerfectSquare(2); // return false
</code></pre>
<p>
Fill out the function such that it can be used anywhere to determine if a number is a perfect square or not. Test it by running it many times with both numbers that are and aren't perfect squares.
</p>
<h2>Multiples of numbers</h2>
<p>
Write a function that takes as input a number, and returns a list of the first 20 multiples of that number.
</p>
<pre><code>// Input: 4
// Output: [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80]
// Input: 17
// Output: [17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 272, 289, 306, 323, 340]</code></pre>
<p>
<b>Bonus:</b> How would you change this function to return a list of the first 10 powers of that number? <i>Hint:</i> Check out the <a href="http://www.w3schools.com/jsref/jsref_pow.asp">JavaScript <code>pow()</code> Method</a>.
</p>
<h2>What does this function do?</h2>
<p>
Please do <b>NOT</b> put this into repl.it, I do <b>NOT</b> want you to actually run it until you have figured it out! Trust me, you'll appreciate the practice. Instead test yourself by stepping through it line by line yourself. I suggest taking out a pen(cil) and paper or using sublime as a notepad to keep track of variable values as you go line by line
</p>
<pre><code>const MysteryFunction = function(input1, input2) {
return MysteryFunction2(input1) + MysteryFunction2(input2)
}
const MysteryFunction2 = function(input) {
if (input > 10) {
return 10
} else {
return input + 10
}
}
// What does it log?
console.log(MysteryFunction(15, 7))</code></pre>
<h2>Let's try a harder one</h2>
<pre><code>const MysteryFunction = function(input) {
if (MysteryFunction2(input % 5) > 10) {
return MysteryFunction2(input % 3)
}
return MysteryFunction2(input % 4)
}
const MysteryFunction2 = function(input) {
return input * 3
}
// What does it log?
console.log(MysteryFunction(23))</code></pre>
<h2>Here's one with loops</h2>
<pre><code>const MysteryFunction = function(input) {
if (input.length > 4) {
return MysteryFunction2(input, 3)
}
return MysteryFunction2(input, 2)
}
const MysteryFunction2 = function(input1, input2) {
for (let i = input2; i >= 0; i--) {
input1.push(i)
}
return input1
}
// What does it log?
console.log(MysteryFunction([4,5,6]))
// What about this call?
console.log(MysteryFunction([10,9,8,7,6])</code></pre>
</div>
</div>
</div>
</body>
</html>