Show Menu
Cheatography

Javascript Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Question 2

let dividend = +prompt("Insert a dividend.");
let divisor = +prompt("Insert a divisor.");

if ((dividend % divisor) === 0) {
	alert(The quotient is ${dividend / divisor}.);
} else {
	alert(The quotient is ${Math.floor(dividend / divisor)} and the remainder is ${dividend % divisor}.);
}
Write a script that takes in a dividend and a divisor. With those, alert either the quotient by itself OR the quotient and the remainder if applic­able.

Question 3

function cuts(input) {
	let cuts = 0;
	while (input > 1) {
		input /= 2;
		cuts++;
	}
	return cuts;
}
Write a function that takes in a number an integer and divides it in half until it becomes 1 or less.

Question 4

function listElements(a, n) {
	if (a.length > n) {
  	let result = [];
    for (let i = 0; i < n; i++) { 
    	result.push(a[i]);
    }
    return result;
  } else {
  	return a;
  }
}
Write a function that takes in array a and returns a new array consisting of the first​ ​n elements of the original array. If n is greater than a, then simply return a copy of array a.

Question 5

function compareArrays(a, b) {
	if (a.length !== b.length) {
		return false;
	}
	for (let i = 0; i < a.length; i++) {
		if (a[i] !== b[i]) {
			return false;
		}
	}
	return true;
}
Write a function that takes in two arrays, a and b, and returns whether or not the listed elements are equal. Assume that all values are primitive (and not Objects) and that the values must be in the same order. It should also output false if the parameters are not arrays.

Loops

"­for­" loop
for (begin; condition; step) {
 
// ... loop body ... }
 
}
"­whi­le" loop
while (condi­tion) {
 
//code
 
//loop body (i++)
 
}
 

Question 6

function capitalizeMiddleCharacter(inputString) {
	let middleLetterPosition = Math.ceil(inputString.length / 2);
	return inputString.substring(0, middleLetterPosition) 
	+ inputString.charAt(middleLetterPosition).toUpperCase() 
	+ inputString.substring(middleLetterPosition + 1, inputString.length);
}
Write a function which takes in a String and capita­lizes the middle letter of the string. If the string is even, then capitalize the character to the right of the middle.

Question 7

function suffixes(inputWord) {
	let result = [""];
	let lastSuffix = "";
	for (let i = inputWord.length - 1; i >= 0; i--) {
		lastSuffix = inputWord.charAt(i) + lastSuffix;
		result.push(lastSuffix);
	}
	return result;
}
Write a function which takes in a word and returns an array containing successive suffixes of the word, starting with the last character.

Question 8

function student(name, birthday, email, ID, SSID, Major, Minor, enrolled, graduate) {
	this.name = name;
	this.birthday = birthday;
	this.email = email;
	this.ID = ID;
	this.SSID = SSID;
	this.Major = Major;
	this.Minor = Minor;
	this.enrolled = enrolled;
	this.graduate = graduate;
}
Write an Object constr­uctor or Class of a human being. It would also be helpful to draw the Object diagram of a few of these.

Presedence