Do you like spaced repetition learning? Have you used Anki or Quizlet? Whether or not spaced repetition works for you, periodically working on flash-card like questions can be a lot of fun, and just may help you retain information. Here are a few problems tied to the course material. Visit them periodically, and feel free to use them in your own spaced repetition learning practice!
setup
and draw
?setup
is called once, and then draw
is (normally) called over and over again.background
function do?setup
?createCanvas
. Many P5 variables are not available, and many functions won’t work at all, until createCanvas
is called.line(0, 0, width, height)
line(width, 0, 0, height)
stroke('red'); fill('green'); rect(20, 20, width-40, height-40)
strokeWeight
noStroke()
.const
and let
in JavaScript?let
can be re-bound to different values; those bound with const
cannot.draw
.mouseIsPressed
and mousePressed
?background
commented out, and (b) uncommented.function draw() { // background('white') line(pmouseX, pmouseY, mouseX, mouseY) }
a.push(x)
ball
to the top of the canvas if the ball has gone below the bottom edge of the canvas?if (ball.y > height) { ball.y = 0 }
Stay Tuned
Many more to follow.
|
|
1 - 2 - (3 - 4 - 5) - 6
! x && y && z
a < b > c == d > e
typeof + 3
first-guess
a legal variable name in JavaScript? If not, why do you think such a
name was banned? (Answer using complete sentences. Back up your answer. Concrete examples will
most likely be helpful.)'dog'.charAt(2).charAt(0)
and explain the result.let p1 = {name: 'Alice'}; let p2 = {name: 'Bob', manager: p1}; p1.manager = p1;What is the value of
p2.manager.manager.manager.name
?
let sum;
for (int i = 1; i < 10000; i++) {
sum += i;
console.log(`The sum from 1 to 10000 is ${sum}`);
}
a
after executing
the following:
let a = [1,2,3,4]; a.unshift(a.pop());
let x = 1; let f = (y) => { console.log(x + y); } f(2);What would happen if the parameter $y$ were renamed to $x$?
function swapFirstTwoElementsOf(a) { let oldFirstElement = a[0]; a[0] = a[1]; a[1] = oldFirstElement; }This works. Then your friend comes along and says “oh hang on, I can do better” and writes this:
function swapFirstTwoElementsOf(a) { a = [a[1],a[0]].concat(a.slice(2, a.length)); }Does your friend’s function work? Why or why not? Be very specific. Use precise, technical language.
kilos
and meters
in the last line with their definitions from the first two lines? Why or why not?
const kilos = prompt('Enter your weight in pounds') * 0.45359237; const meters = prompt('Enter your height in inches') * 0.0254; alert(`Your body mass index is ${kilos / (meters * meters)}`);
let total = 1000; let year = 0; while (total < 5000) { year = year + 1; total = total * 1.05; } console.log(`It will take you ${year} years to get there.`);
Modify the script so that it first prompts for the APR and then computes the amount of time required to grow 1000 dollars to 5000 dollars where the rate is compounded monthly. You may print the result in either months or years.
const capitals = { ME: 'Augusta', NH: 'Concord', VT: 'Montpelier', MA: 'Boston', CT: 'Hartford', RI: 'Providence' };So if your user inputs
NH
, your script should print Concord
. If you want a point of extra credit, allow the user to enter data in any case, for example NH, Nh, nH, or nh for New Hampshire.
dollarMessage
that has one parameter representing a number of dollars and returns
a string stating how many dollars this is. Here are some test cases:
dollarMessage(5) ⟶ "5 dollars" dollarMessage(1) ⟶ "1 dollar" dollarMessage(7.244) ⟶ "7.244 dollars"Use a conditional expression, not an if-statement, to distinguish between writing
dollar
versus dollars
.
sign
that takes one parameter, a number, and returns -1 if the number is negative, 0 if the number is 0, 1 if the number is positive, and NaN if the parameter is not a number. Use an if-statement.
isSmallInteger
which returns true
if its sole parameter is an integer between -128 and 127, inclusive, and false
otherwise. Your function body should have a single return statement in which you test that your parameter is not NaN, is an integer, is greater than or equal to -128, and is less than
or equal to 127.
isSmallInteger('dog') ⟶ false isSmallInteger(-129) ⟶ false isSmallInteger(-128) ⟶ true isSmallInteger(127) ⟶ true isSmallInteger(128) ⟶ false isSmallInteger(14.8) ⟶ false isSmallInteger(14.0) ⟶ true
isShortInteger
which returns true
if its sole parameter is an integer between -32768 and 32767, inclusive, and false
otherwise. Your function body should have a single return statement in which you test that your parameter is not NaN, is an integer, is greater than or equal to -32768, and is less than or equal to 32767.
sumOfRange
that takes in two parameters, $x$ and $y$, which should both be small integers (see the previous problem), and returns the sum of all of the integers in the range from $x$ (inclusive) to $y$ (exclusive). If either of $x$ or $y$ is not a small integer (call the function from the previous problem to check this!!!) then throw an exception. Example test cases for you:
sumOfRange(5, 9) ⟶ 5 + 6 + 7 + 8 = 26 sumOfRange(-2, 4) ⟶ -2 + -1 + 0 + 1 + 2 + 3 = 3 sumOfRange(9, 5) ⟶ 0 (because there are no numbers >=9 AND <5) sumOfRange(-99999, 0) ⟶ throws an exception sumOfRange(14, 'dog') ⟶ throws an exception
sumOfCodepoints
which takes a string and returns the sum of each of the codepoints in the string.
squares
that takes in an array and returns a new array containing all the squares of the input array. Test cases:
squares([1,7,-4]) ⟶ [1,49,16] squares([]) ⟶ [] squares([true, -6, null]) ⟶ [1, 36, 0]
square
that takes in an array of numbers and changes this array by replacing each element with its square. Test cases:
let a = [1,7,-4]; square(a); a ⟶ [1,49,16]
halfsies(1) ⟶ 0 halfsies(0) ⟶ 0 halfsies(-22.7) ⟶ 0 halfsies(2) ⟶ 1 halfsies(19) ⟶ 5 halfsies(7.225E102) ⟶ 342
lengthOfCollatzSequence(1) ⟶ 1 lengthOfCollatzSequence(3) ⟶ 8 lengthOfCollatzSequence(4) ⟶ 3 lengthOfCollatzSequence(16) ⟶ 5
isLeapYear(2000) ⟶ true isLeapYear(1940) ⟶ true isLeapYear(2100) ⟶ false isLeapYear(1987) ⟶ false isLeapYear(1381) ⟶ (throws an exception)
stripAsciiVowels('Hello, world') ⟶ 'Hll, wrld'
scramble('Hello, world') ⟶ 'w,dlroH elol'
powersOfTwo(70) ⟶ [1,2,4,8,16,32,64]
stutter([5,true,[3],'ha']) ⟶ [5,5,true,true,[3],[3],'ha','ha'] stutter([{}, null]) ⟶ [{}, {}, null, null]
prefixes('eich') ⟶ ['', 'e', 'ei', 'eic', 'eich'] prefixes('') ⟶ ['']
powers(3, 400) ⟶ [1,3,9,27,81,243] powers(-2, 17) ⟶ throws an exception powers(3, -9) ⟶ [] powers(0.5, 22) ⟶ throws an exception
interleave(['a', 'b'], [1, 2, true, nil]) ⟶ ['a', 1, 'b', 2, true, nil] interleave(['a', 'b'], []) ⟶ ['a', 'b']
|
characters. Each pair has its two components separated internally with commas. You need to create a real JavaScript object from the string by treating each pair as a property name together with its value. If that was hard to understand, just "figure it out" from the following examples:
objectify('a,dog|b,cat|z,rat') ⟶ {a: 'dog', b: 'cat', z: 'rat'} objectify('') ⟶ {} objectify('one,uno|two,dos') ⟶ {one: 'uno', two: 'dos'}
take
which takes an array $a$ and a number $n$ and returns a new array consisting of the first $n$ elements of $a$. If $n$ is greater than the length of $a$, return a copy of $a$.drop
which takes an array $a$ and a number $n$ and returns a new array consisting of all but the first $n$ elements of $a$. If $n$ is greater than the length of $a$, return an empty
array.sift("javascript", 2) ⇒ "aacit" sift("python", -4) ⇒ throws an exception sift("coffeescript", 5) ⇒ "offescrit" sift("java", isNaN) ⇒ throws an exception