Javascript Exercises
Javascript Exercises
function towerBuilder(nFloors) {
let tower = [];
let index = 0;
//loop through each floor
while (index < nFloors){
let level = '';
//loop left spaces
for (let i = 0; i < nFloors-1-index; i++){
level += ' ';
}
//loop stars
level += '*';
let q = 0;
while (q < index){
level += '*';
level += '*';
q++;
}
//loop right spaces
for (let i = 0; i < nFloors-1-index; i++){
level += ' ';
}
function isUpperCase(letter) {
return letter === letter.toUpperCase();
}
function findMissingLetter(array) {
let key = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
//loop through array to find difference
for (let i = 0; i < array.length - 1; i++) {
let start = key.indexOf(array[i].toLowerCase());
if (array[i + 1].toLowerCase() != key[start + 1]) {
//return difference
//account for case difference
if (isUpperCase(array[0])){
return key[start + 1].toUpperCase();
}
return key[start + 1];
}
}
}
function getMatrixProduct(a, b) {
//lengths have to match
if (a[0].length != b.length) {
return null;
}
return result;
}
class PaginationHelper {
constructor(collection, itemsPerPage) {
// The constructor takes in an array of items and a integer indicating how many
// items fit within a single page
this.collection = collection;
this.itemsPerPage = itemsPerPage;
}
itemCount() {
// returns the number of items within the entire collection
return this.collection.length
}
pageCount() {
// returns the number of pages
let totalItems = this.collection.length;
let itemsPerPage = this.itemsPerPage;
let pages = 0;
while(totalItems > 0){
pages++;
totalItems = totalItems - itemsPerPage;
}
return pages;
}
pageItemCount(pageIndex) {
// returns the number of items on the current page. page_index is zero based.
// this method should return -1 for pageIndex values that are out of range
let totalItems = this.collection.length;
let itemsPerPage = this.itemsPerPage;
if (pageIndex >= this.pageCount() || pageIndex < 0) {
return -1;
}
if(pageIndex == this.pageCount()-1){
let answer = totalItems%itemsPerPage;
if(answer != 0){
return answer;
}
return itemsPerPage;
}
return itemsPerPage;
}
pageIndex(itemIndex) {
// determines what page an item is on. Zero based indexes
// this method should return -1 for itemIndex values that are out of range
let totalItems = this.collection.length;
let itemsPerPage = this.itemsPerPage;
let pages = 0;
let curr = itemsPerPage;
if (itemIndex >= this.collection.length || itemIndex < 0) {
return -1;
}