Welcome to BHS Computer Science. If you are a student in the class, the first thing you need to do (and which we should have done in class) is set up your GitHub account.
Once you have a GitHub account, click “Log in to GitHub” below to proceed. Or you can click “Use anonymously” to play with the environment but you will not be able to save your work or submit assignments. (You can always log in later by clicking the at the top of the page.)
If you don’t have a GitHub account yet, please create one and then log in here for further instructions.
Congratulations! You have successfully connected this app to GitHub. However you are not yet a member of the GitHub organization for this class, something Mr. Seibel needs to set up for you.
This is your GitHub profile URL:
Click the clipboard icon to copy it and then submit it at this form so he can add you.
Congratulations! You have successfully connected this app to GitHub. And it looks like you have an invitation to join the GitHub organization for this class. You need to accept that invitation before you can proceed. The invite should be sent to whatever email you used when you created your GitHub account.
I see you are logged into GitHub and a member of the berkeley-high-cs GitHub organization. However there seems to have been some problem finishing the setup for your account. Please let Mr. Seibel know.
This is a tool for the BHS Computer Science class at Berkeley High School. It is intended to provide a simple environment for experimenting with Javascript without all the complexities of a full development environment such as ReplIt or Glitch which we may use later in the year.
It is also designed to take advantage of the browser’s ability to run Javascript natively. It does not need access to a server to run code making in extremely responsive even if the Wifi is flaking out.
Finally, under the covers it is saving work to a GitHub repository in a very simplified workflow that does not depend on immediately learning any git commands. Code written in this environment for each assignment is saved to a directory and branch specific to that assignment each time it is saved. Thus when the assignment is done, it is easy to go to GitHub and create a PR containing just the work on that assignment which can then be commented on and worked on further before it is turned in and merged to main.
You're all set! You don't need to worry about this yet but we have successfully created a GitHub repository for your work:
You can get to it any time by clicking on your GitHub username at the top-right of the screen.
This exam contains all the questions from past assessments. However you do not need to attempt all of them!
You should instead answer whatever questions you think will best demonstrate your understanding of the standards on which you want to improve your grade.*
Your exam grade for each standard will be based on the proportion of attempted questions covering the standard that you answer correctly. While your exam grades can only help your final semester grades, you must answer at least five questions covering a standard to get an exam grade for that standard.
The functions are grouped by standard and the buttons in the questions navigation bar let you jump to different groups of questions. Additionally, the standards covered by each question are indicated next to the text of the question. Note that some questions cover multiple standards.
Unlike recent assessments, it is a completely closed book assessment meaning no index cards or other notes. Please close all other browser tabs before starting the exam. There should be no talking during the exam.
When you are done, please submit a GitHub pull request of the
branch and request me as a reviewer.
You may also use exam time to finish merging approved PRs after you
are done with the exam. This is your last chance to improve your
Github grade.
You can show and hide these instructions after you begin by clicking the in the questions navigation bar.
Good luck and happy hacking!
* The questions are categorized into six of our eight standards. The Functions standard is measured by all questions and your Github grade is based on the state of all your pull requests at then end of the final exam period.
Write a function named
totalEggs
that takes two
arguments, a number of hard-boiled eggs and a number of
soft-boiled eggs you want to make and returns the total number
of eggs you need.
Write a function named
chocolatesPerPerson
that takes two
arguments, a number of chocolates and a number of people. Return
the largest whole number of chocolates you can give to each
person if everyone gets the same amount. For instance,
chocolatesPerPerson(13, 5)
should return
2
.
Write a function named
extraChocolates
that takes two
arguments, a number of chocolates and a number of people. Return
the number of chocolates that are left over after distributing
the chocolates evenly to all the people. For instance, For
instance, extraChocolates(13, 5)
should return
3
.
Write a function named
leftOut
that takes two arguments,
a number of chocolates and a number of people. Return the number
of people who won’t get a chocolate if you try to distribute
them evenly and there are more people than chocolates. However,
if there are enough chocolates for everyone to get at least one
the function should return 0. For example,
leftOut(10, 13)
should return 3
but
leftOut(13, 10)
should return 0
.
Write a function named
probabilityAllHeads
that takes a
single argument specifying the number of times a coin will be
flipped and returns the probability (a number between 0 and 1,
inclusive) of getting all heads. For instance
probabilityAllHeads(1)
should return
0.5
since the chance of getting heads on one toss
is 1/2. And probabilityAllHeads(2)
should return
0.25
since the chance of two independent
outcomes—such as getting a head on each of two flips of a
coin—both occuring is the product of their probabilities, in
this case 0.5 × 0.5 = 0.25. For reference
probabilityAllHeads(10)
should return
0.0009765625
.
Write a function named
futureHour
that takes two
arguments, the current hour on a 24-hour clock, i.e. a number
from 0-23 inclusive, and a positive number of hours in the the
future that an event will occur. Return the hour it will be when
the event occurs. For example,
futureHour(9, 4)
should return
13
since four hours after 9 it will be hour 13.
Note that the number of hours can be arbitrarily large:
futureHour(9, 28)
would also return
13
since the event would occur at hour 13 the next
day.
Write a function named
presentsBudget
that takes two
arguments, the number of friends you are buying presents for and
the average price of present you plan to buy, and returns the
total amount of money you expect to spend. For instance
presentsBudget(6, 15)
, i.e. six friends on whom you
want to spend an average of fifteen dollars, should return
90
.
Write a function named perPresent
that takes two arguments, the total amount of money you have
budgeted for buying presents and the number of presents you need
to buy, and returns the average amount you can spend per
present. Thus
perPresent(90, 6)
should return
15
since with a budget of ninety dollars and six
presents to buy, you can spend an average of fifteen dollars per
present.
Write a function named
wrapingCombos
that takes three
arguments, the number of kinds of wrapping paper you have, the
number of kinds of ribbions you have, and the number of kinds of
decorative bows you have. Return the number of different
combinations of paper, ribbon, and bow you could produce, using
one kind of paper, one kind of ribbon, and one bow to wrap a
present. For example, with just two kinds of paper, three kinds
of ribbon, and five different bows there are thirty combinatons:
for the two kinds of paper you can choose any of the three
ribbons, giving six paper/ribbon combinations. And each of those
combinations can be topped with one of five different bows,
giving thirty total combinations. Thus
wrappingCombos(2, 3, 5)
should return
30
.
Write a function named
biggestNumber
that takes a single
argument representing a number of digits and returns the largest
number that can be written in our ordinary decimal (base-10)
number system using that many digits. For instance:
biggestNumber(1)
should return 9
biggestNumber(2)
should return 99
biggestNumber(3)
should return 999
Hint: As a starting point, you might want to think about the numbers one bigger than these numbers and how you could express them in terms of the desired number of digits.
Write a function named
itemsLeftOver
that takes two
arguments, a number of people and a number of items, and returns
the number of items that will be left over after you give each
person the maximum number of items you can while giving everyone
the same number of items.
Write a function named
areaOfCircle
that takes one argument, the radius of a circle, and returns the
area of the circle. The formula for the area of a circle with
radius
r is πr2. In Javascript you can use
Math.PI
to get a good approximation of π.
Write a function named
volumeOfCube
that takes a single
argument specifying the length of one edge of a cube, and
returns the volume of the cube.
Write a function named
populationGrowth
that takes two
numeric arguments, the current size of a population and a growth
rate expressed as a fraction the population will grow in a day.
The function should return the amount by which the population
will grow in one day. For example, if the population was
initially 100 and the growth rate was 0.25, it would grow by 25
members.
Write a function named
earnedRunAverage
that takes two
arguments, the number of “earned runs” a baseball pitcher has
given up and the number of innings that pitcher has pitched. The
function should return the pitcher’s Earned Run Average (ERA)
which is defined as the average number of earned runs per inning
pitched multiplied by 9.
Write a function named
valueOfJewels
that takes four
arguments, a number of diamonds, a number of emeralds, the value
of one diamond in gold pieces, and the value of one emerald in
gold pieces. The function should return the value in gold pieces
of the given number of diamonds and emeralds. In other words
multiply the number of each type of jewel by the value of that
kind of jewel and sum the products.
Write a function named
payWithOvertime
that takes three
numeric arguments, a number of hours someone worked, their
normal hourly rate, and their overtime rate. The function should
return how much they are paid for the hours worked assuming that
they are paid their normal rate for the first eight hours and
their overtime rate for any hours beyond that.
Write a function named
firstClassPostage
that takes one
argument, the weight in ounces of a letter. It should return the
postage needed, in cents, to mail the letter given that anything
up the first ounce costs 60 cents and each additional ounce, or
fractional ounce, costs 24 cents. A function that will be useful
is Math.ceil
which returns the smallest integer
greater than or equal to its single argument. For instance,
Math.ceil(2.3)
is 3.
Write a function named
weightOnJupiter
that takes a
single argument, the weight of a person in kilograms on Earth,
and returns the weight of that same person on Jupiter.
Note that weights on other planets are computed by multiplying
the Earth weight by the ratio of the other planet’s gravity to
Earth’s gravity. For instance, if there was a planet whose
gravity was exactly twice Earth’s, then someone would weight
exactly twice as much on that planet as on Earth. Two useful
constants defined in the starter code for you are
JUPITER_GRAVITY
and EARTH_GRAVITY
Write a function named
gravity
that takes three numeric
arguments, the first two are the masses of two bodies (such as
planets) and the third is the distance between the two objects.
It should return the gravitational force attracting the two
bodies which is computed as the product of the bodies’ masses
divided by the square of the distance between them, all
multiplied by the universal gravitational constant which is
defined for you in the starter code as the constant
G
.
Write a function named
averageWeight
that takes two
arguments, the total weight of a number of items and the number
of items, and returns the average weight of an item. E.g. if the
total weight is 1,000 and there are 20 items, the average weight
is 50.
Write a function named
hypotenuse
that takes two
arguments representing the lengths of the two legs of a right
triangle and returns the length of the hypotenuse. Recall the
Pythagorean theorem that tells us that c2 = a2
+ b2 where a and b are the lengths of
the two legs of a right triangle and c is the length of
the hypotenuse.
Write a function named
maxRadius
that takes two
arguments, the first being the width and the second being the
height of a drawing area. It should return the radius of the
largest circle that can be drawn in the drawing area.
Write a function named
numCircles
that takes two
arguments, the radius of a circle and the width of a drawing
area, and returns the largest integer number of non-overlapping
circles of the given radius that can be drawn in a line across
the drawing area..
Write a function named offset
that
takes two arguments, the width, in pixels, of a drawing area and
the width of a figure (guaranteed to be less than the total
width, also in pixels) and which returns the number of pixels
from the left side of the drawing to shift the figure to be
horizontally centered in the drawing area.
The BHS fire alarms go off if a fire alarm is pulled, if smoke
is detected, or if there is a planned fire drill. Write a
function named fireAlarm
that
takes three arguments and returns a boolean value indicating
whether the fire alarm should go off.
The values of the arguments will be booleans indicating, in order, whether a fire alarm was pulled, whether smoke was detected, and whether there is a planned fire drill.
There are three criteria for a person to be eligible to be elected president of the United States: they must be at least 35 years old, they must be a natural born citizen, and they must have lived in the US for at least fourteen years. A person must meet all these criteria to be eligible.
Write a function named
canBePresident
that takes three
arguments, and returns true if a person can be president. The
first argument is the persons age in years; the second, a
boolean indicating whether or not they are a natural born
citizen; and the third, the number of years they’ve lived in the
US.
On Twitter, a user should see a tweet if they follow the person who tweeted it or if they follow a person who retweeted it. However they should never see a tweet from someone they have blocked.
Write a function named
willSeeTweet
that returns a
boolean indicating whether a user should see a given tweet
according to the values of its three boolean arguments. The
first says whether they follow the tweeter; the second, whether
they follow someone who retweeted the tweet; and the third,
whether they have blocked the original tweeter.
Write a function named
needHeavyCoat
that takes two
boolean arguments, one that is true if a person is going outside
and the other that is true if they are somewhere cold, and
returns a boolean indicating whether they need a heavy coat
which they only need if they are going outside somewhere cold.
Write a function named
needSunscreen
that takes two
boolean arguments, one that is true if someone is going to the
beach and the other that is true if they are going skiing, and
returns a boolean indicating whether they need to wear sunscreen
which they need to do both at the beach and on the ski slopes.
Write a function named
needMittens
that takes two boolean
arguments, one which is true if a person is going outside and
the other which is true if they are somewhere warm, and returns
a boolean indicating whether they need to wear mittens which
they need if they are going outside somewhere that isn’t warm.
Write a function named
isVenomous
that returns a boolean
indicating whether a snake on the island of Booleana is venomous
based on two boolean arguments, one which is true if the snake
is striped and the other of which is true if the snake has a
blue head. All striped snakes on Booleana are venomous as are
all of the unstriped snakes on the island except the blue-headed
Blooby Snake.
Write a function named
isMagicNumber
that takes a single
numeric argument and returns true if the number is one of the
two magic numbers, 42
or 17
. (For
10,000,000 extra credit points, include a comment about why you
think 42 and 17 are magic numbers.)
Write a function named canSleepIn
that takes two boolean arguments, the first saying whether it's
a weekday and the second saying whether you're on vacation, and
returns
true
if you are allowed to sleep in and
false
otherwise. The rules are you can always sleep
in when you're on vacation and you can also sleep in on
weekends.
Write a function named
canGoToProm
which takes three
boolean arguments describing a person. The first specifies if
they are a senior, the second specifies if they were invited to
prom by a senior, and the third specifies if they are on the
prom exclusion list. The function should return a boolean
indicating whether or not the person is eligible to go to prom.
The rules are seniors and people invited by seniors are eligible
unless they are on the exclusion list.
Write a function named
getsSpeedingTicket
that takes two
arguments, a number indicating the speed in miles per hour that
you were driving and a boolean indicating whether the cop who
pulled you over is grouchy. Return a boolean value indicating
whether you will get a ticket given that a grouchy cop will give
you a ticket if you are going over 65 while a non-grouchy cop
will only give you a ticket if you're going over 70 mph.
Write a function named
evenGreaterThanZero
that takes a
single numeric argument and returns a boolean indicating whether
the number is an even number greater than zero.
The rules for leap years are more complex than you may know. The
basic rule is that all years evenly divisible by 4 (i.e.
year % 4 === 0
) are leap years. However, years
evenly divisible by 100 are not leap years
unless they are evenly divisible by 400. Thus 2004 was
a leap year by the normal rule but 2100 will not be because it’s
divisible by 100. But 2000 was because although it was divisible
by 100 it was also divisible by 400.
Write a function named
isLeapYear
that takes a single
argument of a year number and returns a boolean indicating
whether or not it is a leap year.
Write a function named
okaySpeed
that takes two numeric
arguments, one indicating the posted speed limit in miles per
hour and the other the speed a car is going, also in miles per
hour. It should return true if and only if the speed is no more
than ten mph below the posted limit and no more than ten mph
above the posted limit.
Write a function named
twiceAsExpensive
that takes two
numeric arguments, each indicating the price of an item you are
considering buying. The function should return true if and only
if the first item costs more than twice as much as the second
item.
Write a function named
winningRecord
that takes three
numeric arguments, a team’s number of wins, losses, and ties. It
should return true if the team has a winning record, meaning it
has won more than half of the games it has played.
Write a function named
aFartherThanB
that takes three
arguments, all numbers and returns a boolean indicating whether
the first argument is farther away (on the number line) from the
third argument than the second argument. I.e. if we call the
arguments a, b, and c, return true
if a is farther
away from c than b is.
Write a function named
firstFewEveryOther
that takes a
single string argument that is at least five characters long and
returns a string consisting of just the first, third, and fifth
characters of the argument string. E.g. called with
'foobar'
it should return 'foa'
.
Write a function named
firstHalf
that takes a single
string argument and returns the first half of the string. (If
the string has an odd number of characters it doesn't matter
whether or not you include the extra character.) For instance
the first half of 'foobar'
is
'foo'
but if the argument was
'fooquux'
then either 'foo'
or
'fooq'
would be acceptable return values.
Write a function named
secondHalf
that takes a single
string argument and returns the second half of the string. (If
the string has an odd number of characters it doesn't matter
whether or not you include the extra character.) For instance
the second half of 'foobar'
is
'bar'
but if the argument was
'fooquux'
then either 'quux'
or
'uux'
would be acceptable return values. For
maximum style points write this function and
firstHalf
so that
firstHalf(s) + secondHalf(s)
gives you back
s
.
Write a function named
swapFrontAndBack
that takes a
single string argument and returns a string consisting of the
second half of the original string followed by the first half.
If the string is an odd number of characters it doesn’t matter
whether the extra character goes with the front or the back.
Write a function named upDown
that
takes a single string argument and returns a string consisting
of the original string all in upper case concatenated (“smooshed
together”) with the string all in lower case. E.g. called with
'foo'
it should return 'FOOfoo'
.
Write a function named
upDownLastCharacter
that takes a
single string argument that is at least one character long and
returns a string consisting of two characters, the uppercase
version of the last character of the argument string and the
lowercase version of that same character. E.g. called with
'foo'
it should return 'Oo'
.
Write a function named
firstAndLast
that takes a single
string argument (which will be at least two characters long) and
returns a string consisting of just the first and last
characters of the string.
As you should recall from the Pig Latin assignment, the rules of simple Pig Latin say that a word is translated into Pig Latin by taking everything up to (but not including) the first vowel and moving it to the end of word and then adding “ay” to the end.
Write a function named
simplePigLatin
that takes two
arguments, a string containing a single word and a number
indicating the index of the first vowel in the word, and returns
the word translated into Pig Latin.
Write a function named
randomCharacter
that takes a
single string argument and returns a random character from the
string. For this function you will need to use the
rand
function defined at the top of the starter
code.
Write a function named
randomCharacterUpDown
that takes a
single string argument and returns a string consisting of a
single random character repeated twice, once in upper case and
then in lower case. For this function you will need to use the
rand
function defined at the top of the starter
code.
Write a function named
isAllUpperCase
that takes a single
string argument and returns a boolean indicating whether the
string is all upper case.
Write a function named
sameIgnoringCase
that takes two
string arguments and returns a boolean indicating whether they
are the same string if you ignore case differences.
Write a function named
firstName
that takes a single
string argument which will consist of a full name in the form
first name, a space, and the last name and returns a string
containing just the first name. You will probably want to use
the indexOf
method.
Write a function named
lastName
that takes a single
string argument which will consist of a full name in the form
first name, a space, and the last name and returns a string
containing just the last name. You will probably want to use the
indexOf
method.
Write a function named
initials
that takes a single
string argument which will consist of a full name in the form
first name, a space, and the last name and returns a string
containing the person's initials, i.e. the first letter of their
first name followed by the first letter of their last name. You
will probably want to use the indexOf
method.
Write a function named upToX
that
takes a single string argument and returns a string consisting
of the characters of the original string up to but not including
the first 'x'
. For instance
upToX('quixotic')
should return 'qui'
.
Write a function named
charactersAround
that takes two
arguments, a string and an index into the string and returns a
string consisting of two characters, the one immediately before
the character at the given index and one and the one immediately
after the character at the given index. You can assume that
those characters exist, i.e. the index is not at the beginning
or end of the string. For instance
charactersAround('programming', 4)
should return
'ga'
.
Write a function named middle
that
takes a single string argument and returns a sting consisting of
the the middle of the argument, defined as the original string
with the first and last quarter removed. You can assume the
number of characters in the string is a multiple of four. For
instance middle('abcdefgh')
should return
'cdef'
.
Write a function named pair
that
takes two string arguments and returns a single string
consisting of the two arguments separated by the word
and
. For instance
pair('peanut butter', 'jelly')
should return
peanut butter and jelly
.
Write a function named
containsX
that takes a single
string argument and returns true
if it contains an
'x'
and false
if it does not. For
instance containsX('flexible')
should return
true
and containsX('smooth')
should
return false
.
Write a function named slug
that
takes three string arguments and returns a “slug” consisting of
the three strings joined together with hyphens and all in lower
case. For instance slug('Foo', 'Bar', 'BAZ')
would
return 'foo-bar-baz'
.
Write a function named capitalize
that takes a single string argument and returns a string with
the same characters but with the first character in upper case
and all remaining characters in lower case. For instance
capitalize('programming')
should return
'Programming'
.
Write a function named
capitalizeName
that takes a single
string consisting of a first and last name separated by a space
and returns a string with the first and last name separated by a
space but capitalized using the capitalize
function
you just wrote. For instance
capitalizeName('fred flintstone')
should return
'Fred Flintstone'
. Note: you must use
capitalize
in this function to get full credit.
Write a function named
moreThanTwiceAsLong
that takes two
string arguments and returns a boolean indicating whether the
first string contains more than twice as many characters as the
second string.
Write a function named
yesIfEven
that takes a single
argument which will be a number and returns the string
'yes'
if the argument is even and
'no'
otherwise.
Write a function named
logIfOk
that takes a single
argument that can be passed to the function
isOk
provided in the starter code to get a boolean
indicating whether the value is “okay”. If the value is okay
according to isOk
you should call the provided
log
function, passing the value so it can be
logged. Note: you do not need to write isOk
or
log
but you can adapt the simple implementations in
the starter code if you’d like, to test your code.
Write a function named
firstOk
that takes a numeric
argument and returns the first integer greater than or equal to
zero and less than the argument that is okay according to the
isOk
function. If none of the numbers in the range
are okay, it should return undefined
.
Write a function named timeToLeet
that repeatedly generates random numbers with the provided
random10k()
function and returns the number of
numbers generated that were not leet, according to the provided
isLeet
function, before a leet number was
generated. (Thus if the very first number generated is leet, it
should return 0. If the 101st number generated was leet, then it
should return 100.)
Write a function named
classify
that takes an arbitrary
value as its single argument and calls one of the provided
functions recordOk
or recordNotOk
with the value, the former if the value is okay according
isOk
and the latter otherwise.
Write a function named
threewayClassify
that takes an
arbitrary value as its single argument and calls one of the
three provided functions recordOk
,
recordMeh
, or recordNotOk
with the
value. It should call recordOk
if the value is okay
according to isOk
, recordMeh
if the
value is meh according to isMeh
, and
recordNotOk
otherwise.
Write a function named
sumOfSquares
that takes a single numeric argument and returns the sum of the
squares of the positive integers less than the argument. For
instance
sumOfSquares(10)
should return 285
.
Write a function named pairs
that
takes a single numeric argument and calls the provided
pair
function with every possible pair of numbers
made up of two numbers each in the range from 1 to the argument.
For instance, pairs(3)
would result in calls to
pair
with the pairs (1, 1)
,
(1, 2)
, (2, 1)
, and
(2, 2)
. Note that order matters so
(1, 2)
and (2, 1)
are different pairs.
Also note: pair
takes two arguments so you just
need to pass the two numbers; you do not need to do anything
else to create a pair.
Write a function named
timesTable
that takes a single
number as its argument and emits a times table of all the
products from 1 × 1 to n × n. Use the emit
function
defined in the starter code to actually emit the different
values. It takes three arguments, the two numbers being
multiplied and their product, and prints them out nicely; you
just need to call it once for each pair of arguments. N.B. that
order matters so, assuming you are emitting a times table that
includes 2 × 3 you need to call both
emit(2, 3, 6)
and emit(3, 2, 6)
.
Write a function named
sumSquares
that takes a single
number argument and returns the sum of the squares of all the
positive integers less than the argument. For instance given the
argument 4
it should return 14
, i.e.
12 + 22 + 32, i.e. 1 + 4 + 9.
Write a function named
countXs
that takes a single string
argument and returns the number of 'x'
characters
that occur in the string.
Write a function named
makeRow
that takes no arguments
and returns a new three-element array where each element is the
empty string, ''
.
Write a function named
makeBoard
that takes no arguments
and returns a 3x3 array. That is, it should return an array
containing three elements, each of which is a new array
containing three elements. The elements of the inner arrays
should all be the empty string, ''
. In other words,
the inner arrays are rows such as are returned by the
makeRow
function you just wrote, which you are free
to use here if you want.
Write a function named
allTheSame
that takes an array of
three elements and returns true
if all three
elements are ===
to each other and
false
otherwise. For instance,
allTheSame(['X', 'X', 'X'])
should return
true
.
Write a function named
extractColumn
that takes a 3x3 array like the ones returned by
makeBoard
and an index from 0 to 2, inclusive, and
returns a three-element array containing the values in the
column specified by the index, assuming the inner arrays
represent rows. For instance if board
is the array:
[
['X', '', ''],
['O', '', ''],
['', 'X', '']
]
then extractColumn(board, 0)
should return
['X', 'O', '']
Write a function named
fillTimesTable
that takes a single
n-by-n 2D array as an argument. That is, the
argument will be an array with n elements each of which
is an array of n elements. The inner arrays will
initially be filled with zeros. The function should replace each
zero with the products from 1 × 1 at
table[0][0]
and n × n at
table[n - 1][n - 1]
and return
undefined
(Note, n itself is not an argument
to the function.)
For instance, if called with the empty table:
[[0 , 0, 0], [0, 0, 0], [0, 0, 0]]
it should leave the table in this state:
[[1, 2, 3], [2, 4, 6], [3, 6, 9]]
Write a function sums
that takes a
single number, n
and returns an array with
n + 1
numbers in it such that each element of the
array is the sum of the index of that element and the preceeding
indices. (The zeroth element should have the value 0.) For
example sums(5)
should return
[0, 1, 3, 6, 10, 15]
. The third item,
3
is 3 because it's at index 2 + the preceeding
value of 1.
Write a function rule110
that
takes an array of arbitrary length containing only 0s and 1s as
its argument and returns a new array of the same length filled
according to the following somewhat complicated rules:
For each index, i
, the value at i
in
the new array is determined by the three values in the original
array at i - 1
(the left value), at
i
(the center value), and at
i + 1
(the right value). When i
is 0
use 0 for the left value and when i
is the last
index in the array, use 0 for the right value.
The i
th value of the result array will be 0 in
three cases: when the left, center, and right values are all 0
or the two cases when the left value is 1 and the center and
right values are the same, i.e. both 0s or both 1s. In all other
cases, the i
th element in the result is 1.
Write a function named
isSamePoint
that takes two point
objects as arguments, each with x
and
y
properties, and returns a boolean indicating
whether the two objects represent the same x, y coordinate.
Write a function named area
that
takes a single argument which will be an object with
width
and height
properties
representing the dimensions of a rectangle and returns the area
of the rectangle as a number.
Write a function named
totalWithTip
that takes two arguments, an object with a property
subtotal
and a number between 0 and 1.0
representing the percentage tip, e.g. 20% tip would be 0.2.
Return a new object that has three properties,
subtotal
with the same value as the argument
object, tip
which is the tip computed from the
subtotal and the given percentage, and total
which
is the total with the tip.
Write a function named
higherPaid
that takes two
arguments, each an object representing an employee. The employee
objects each have a salary
property and the
function should return the object representing the employee with
the higher salary.
Write a function isWinner
that
takes an object with a score
property and returns
true if the score is over 100 and false otherwise.
Write a function updateWins
that
takes an array of objects each with a
score
property and a wins
property and
updates each object by incrementing wins
by one if
the object is a winner according to the
isWinner
function you just wrote. After updating
the objects, updateWins
should return
undefined
.
Write a function bigWinners
that
takes an array of objects each with score
and
wins
properties and returns an array containing the
elements of the argument array whose wins
property
is over 10.
Write a function named
makeMove
that takes three
arguments: a string, specifying a mark (either
'X'
or 'O'
); a number specifying a
row; and a number specifying a column. It should return an
object with three properties, mark
,
row
, and column
, each with the value
of the corresponding argument.
Write a function named placeMark
that takes a 3x3 array like the ones returned by
makeBoard
and an object like the ones returned by
makeMove
and sets the element of the array at the
row and column specified in the move object to the move's mark.
The function does not need to return any specific value but after calling it the array passed as the first argument should be modified. For instance, after:
let board = makeBoard();
let move = makeMove('X', 1, 1);
placeMark(board, move);
the expression board[1][1]
should evaluate to
'X'
.
Write a function named
recordMove
that takes two
arguments: an array and a move object, like the ones returned by
makeMove
. It should add the given move to the end
of the array.
The function does not need to return any specific value but after it is called the array argument should be one longer than it was before the call and the last element of the array should be the object passed as the second argument.
Write a function named
rowForMove
that takes two
arguments: an array of move objects like the ones collected in
recordMove
and a number which is an index into that
array. It should return the row value (a number) of the move at
the given index.
For example after these two lines are executed:
let moves = [];
recordMove(moves, makeMove('X', 1, 2));
the call rowForMove(moves, 0)
should return
1
.
Write a function named placeMoves
that takes two arguments: a 3x3 array like the ones returned by
makeBoard
and an array of moves such as might be
built up by repeatedly calling recordMove
. The
function should call placeMark
to place the mark
for each move in the array of moves in the correct position on
the board array.
The function does not need to return any specific value though
obviously all the calls to placeMark
will have
updated the board array argument. For instance after these lines
are executed:
let moves = [];
let board = makeBoard();
recordMove(moves, makeMove('X', 1, 1));
recordMove(moves, makeMove('O', 0, 0));
recordMove(moves, makeMove('X', 0, 1));
placeMoves(board, moves);
then the board
should be in this state:
[['O', 'X', ''], ['', 'X', ''], ['', '', '']]