Create a fully-specified HTML page (including !doctype and outer-level
html tags) with title and main text "Hello world!" Then, link to a
JavaScript file called "hello.js" that also prints out "Hello world!" to the
browser console.
// contents of "hello.js"
console.log("Hello world!");
Write a function in JavaScript that takes in a number, and returns a boolean
corresponding to whether that number is prime.
let isPrime = (n) => {
for (let i = 2; i < n; i += 1) {
if (n % i === 0) {
return false;
}
}
return true;
};
Let's say you have an array of integers—say [0, 43, -123, 4, -60, 32]—and
you want to find the sum of only the odd integers in the array. Write two
JavaScript programs to do so: one that doesn't use functional programming
constructs (i.e. map, filter, and/or reduce), and one that does.
// Without functional programming
let numbers = [0, 43, -123, 4, -60, 32];
let result = 0;
for (let i = 0; i < numbers.length; i += 1) {
if (numbers[i] % 2 === 1) {
result += numbers[i];
}
}
console.log(result);
// With functional programming
let numbers = [0, 43, -123, 4, -60, 32];
let result = numbers.filter((n) => n % 2 === 1).reduce((a, b) => a + b);
console.log(result);
In this example, "midiNumber" is an integer between 21 and 108, "duration"
is a duration value (to simplify: one of "1n", "2n", "4n", or "8n"), and
"velocity" is an integer between 0 and 127. (Computer musicians often use
"velocity" as a synonym for "volume" or "loudness".)
Create a function makeRandomNote() that returns a note with a random MIDI
number, duration, and velocity within the bounds stated above. You may find
the following function helpful:
/*
* This function gets a random integer between min and max, inclusive. I copied
* it from StackOverflow: https://stackoverflow.com/questions/4959975/
*/
let getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
Using your new makeRandomNote() function, make five random notes, and store
them in an array, sorted by velocity. You may find this array
method
handy. If you can, challenge yourself to use an anonymous function when
sorting the array.
let randomNotes = [];
for (let i = 0; i < 5; i += 1) {
randomNotes.push(makeRandomNote());
}
randomNotes.sort((a, b) => {
if (a.velocity > b.velocity) {
return 1;
} else if (a.velocity === b.velocity) {
return 0;
} else {
return -1;
}
});
Write a function that takes in a note of the form above, and determines
whether or not that note is in the G major scale. (Hint: you may want to
review the material on pitch classes and MIDI numbers.)
let isInGMajorScale = (note) => {
let gMajorScale = [7, 9, 11, 0, 2, 4, 6];
let pitchClass = note.midiNumber % 12;
return gMajorScale.includes(pitchClass);
};
Look at the following snippet from an HTML document.
<button id="toggler">Show/Hide Text</button>
<p id="text">I'm being toggled!</p>
Make it so that when you click on the button, you toggle the text. If you are
using jQuery, look up $().toggle(); if you are not using jQuery, look up CSS
"display". (Also, if you want to cheat, look at the source code for this very
page!)
// With jQuery
$("#toggler").on("click", () => {
$("#text").toggle();
});
// Without jQuery
document.getElementById("toggler").onclick(() => {
let text = document.getElementById("text");
if (text.style.display === "none") {
text.style.display = "block";
} else {
text.style.display = "none";
}
});