0% found this document useful (0 votes)
5 views

Javascript Exercises

The document contains JavaScript functions for various tasks including building a tower of a specified number of floors, finding a missing letter in an array, calculating the product of two matrices, searching for a needle in a haystack, and a PaginationHelper class for managing collections with pagination. Each function is designed to perform specific operations efficiently. The code demonstrates fundamental programming concepts such as loops, conditionals, and object-oriented programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Javascript Exercises

The document contains JavaScript functions for various tasks including building a tower of a specified number of floors, finding a missing letter in an array, calculating the product of two matrices, searching for a needle in a haystack, and a PaginationHelper class for managing collections with pagination. Each function is designed to perform specific operations efficiently. The code demonstrates fundamental programming concepts such as loops, conditionals, and object-oriented programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 += ' ';
}

//add level to the tower


tower.push(level);
index++;
}
return tower;
}

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;
}

let result = [];

//builds the result matrix (size of mulitplied row and cols)


for (let i = 0; i < a.length; i++) {
result[i] = []; //create new row
for (let j = 0; j < b[0].length; j++) {
result[i][j] = 0; //initialzie at zero
}
}

//multiply rows of a by the columns of b


for (let i = 0; i < a.length; i++) { //rows of a
for (let j = 0; j < b[0].length; j++) { //cols of b
for (let k = 0; k < b.length; k++) { //go through each
result[i][j] += a[i][k] * b[k][j];
}
}
}

return result;
}

function search(haystack, needle) {


let result = [];

for (let key in haystack) {


//check if contains the needle
if (typeof haystack[key] == "string" && haystack[key].includes(needle)) {
result.push(key); // Add the key to results if it matches
}
//keep searching if its not the needle
else if (typeof haystack[key] == "object" && haystack[key] !== null) {
let subResults = search(haystack[key], needle).map(subKey => `${key}.${subKey}`);
result = result.concat(subResults); // Accumulate the results
}
}

return result.sort(); //return sorted list


}

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;
}

while (pages < this.pageCount()){


if (curr > itemIndex){
return pages;
}
pages++;
curr += itemsPerPage;
}
return -1;
}
}

You might also like