Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions js/filterLongWords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const exampleData = ["super", "hyper", "mathematical", "thisisalongword", "thisisalongerword", "standard", "forvalhalla"];
const maxLength = 9;

function filterLongWords(arr, maxLength) {
if (typeof(maxLength) != "number" || parseInt(maxLength) != maxLength || maxLength < 0) {console.error("Need a positive number for max length!"); return};
console.log(maxLength);
return arr.filter(word => word.length > maxLength)
}

filterLongWords(exampleData, maxLength);
93 changes: 93 additions & 0 deletions js/fizzbuzz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// The concept of this solution is that the residues of integers
// modulo 15 are periodic (with period 15). The implementation can
// be done in a few ways, but since machine language deals with binary
// numbers, it's possible that the fastest implementation uses
// binary numbers as a filter (because division is expensive!).
// Nevertheless, I'll write here (in 15 min?)
// both the binary and the human-readable version.

// Note: 810092018 in binary is 110000010010010000011000010000
// Split into pairs, 11 00 00 01 00 10 01 00 00 01 10 00 01 00 00
// Notice that there are 15 pairs of bits, which represent:
// 11 - divisible by 15
// 01 - divisible by 3
// 10 - divisible by 5

// To effectively use that large binary string as a filter, we use bitwise
// comparison operators, e.g. the & comparison operator, and bitshift operators
// e.g. the >> and << shift operators.

// The reason why that binary string is written like this "in reverse" is that
// the digits with smaller values are on the right, e.g. in decimal, the digit 7
// has the smallest value in the number 1234127.
'use strict';

const end = 100;
const start = 1;

function bitFizzBuzz(start, end) {
var bFilter = 810092048;
let counter = bFilter & 3;

for (let i = 1; i < start; i++) {
bFilter = bFilter >> 2 | counter << 28;
}

for (let num = start; num <= end; num++){
let counter = bFilter & 3;
let result = 'FizzBuzz';
// if the binary string returns 0 when bitwise compared to 3 (11), then it's neither
// divisible by 3 (represented by 01) nor 5 (represented by 10).
// if the binary string returns 1 when bitwise compared to 3 (11), then it's divisible by 3 (01).
// if the binary string returns 2 when bitwise compared to 3 (11), then it's divisible by 5
// (remember, 10 indicates divisibility by 5).

if (counter === 0) {
result = num;
} else if (counter === 1) {
result = 'Fizz';
} else if (counter === 2) {
result = 'Buzz';
}

// Sadly this algorithm relies on its own iterative nature to seek the correct position in the
// binary filter, and so doing bitFizzBuzz(15) in the console will not work.
// We will need to do something like "for (let i=1;i<upperLimit;i++) {console.log(bitFizzBuzz(i))}"
// to actually use this. Still need to benchmark this vs the normal implementation below though!
// We can of course shift the binary string beforehand to account for this, but *lazy*. Edit: Ah
// heck it, I wrote that thing above.

// Need to either shift the binary filter to the right by 2 digits to prepare for the next iteration, or to reset it if it's done.
Copy link
Author

@ckkok ckkok Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm checking out this discussion function of GitHub... I've never used this before. Seems a bit messy D:)

Note: This explanation needs to be better phrased. The | isn't conventionally referred to by the "or reset the ..." phrase. What it's doing is 3 bitwise operators, shift to the right by 2, then bitwise-OR with the previous result shifted left by 28 (effectively returning the current 2-digit binary filter to all the way to the left for use later).

bFilter = bFilter >> 2 | counter << 28;

console.log(result);
}
console.log("Bitwise implementation");
}


// And now for the normal implementations...
function normalFizzBuzz(start, end) {
let nFilter = [false, false, 'Fizz', false, 'Buzz', 'Fizz', false, false, 'Fizz', 'Buzz', false, 'Fizz', false, false, 'FizzBuzz'];
for (let i = start; i <= end; i++) {
let result = nFilter[(i - 1) % 15]; //This is an expensive operation, but how much more expensive is it in JS compared to lower level languages, and is it expensive enough to merit the bit comparison implementation above?? If we wanted to cheat like in the bit comparison implementation, we wouldn't do this and just increment the index each time, subtracting 15 each time we hit 16 for the fastest possible runtime :D
console.log(((result) ? result : i));
}
console.log("Normal implementation via modulo check");
}

function nFizzBuzz(start, end) { //This is the cheaty implementation described above :D
let nFilter = [false, false, 'Fizz', false, 'Buzz', 'Fizz', false, false, 'Fizz', 'Buzz', false, 'Fizz', false, false, 'FizzBuzz'];
var r = (start % 16) - 1;
for (let i = start; i <= end; i++){
if (r > 14) {r-=15};
console.log((nFilter[r]) ? nFilter[r] : i);
r++;
}
console.log("Normal implementation with only one modulo check :D");
}

// Pick your poison! :D
//bitFizzBuzz(start, end);
//normalFizzBuzz(start, end);
nFizzBuzz(start, end);
18 changes: 18 additions & 0 deletions js/grade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const score = 56;

function grade(testScore) {
if (typeof(testScore) != "number" || testScore < 0 || testScore > 100) {console.error("Need a valid test score between 0 and 100!"); return}
let result = "A";
switch (true) {
case (testScore < 45): {result = "F"; break};
case (testScore < 50): {result = "E"; break};
case (testScore < 55): {result = "D"; break};
case (testScore < 60): {result = "C"; break};
case (testScore < 70): {result = "B"; break};
default:
break;
}
return result;
}

console.log("A score of " + score + " gives you a grade of " + grade(score) + ".");
38 changes: 38 additions & 0 deletions js/phonebook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var phoneBook = {
"Abe": "111-111-1111",
"Bob": "222-222-2222",
"Cam": "333-333-3333",
"Dan": "444-444-4444",
"Ern": "555-555-5555",
"Fry": "111-111-1111",
"Gil": "222-222-2222",
"Hal": "333-333-3333",
"Ike": "444-444-4444",
"Jim": "555-555-5555",
"Kip": "111-111-1111",
"Liv": "222-222-2222",
"Mia": "333-333-3333",
"Nik": "444-444-4444",
"Oli": "555-555-5555",
"Pam": "111-111-1111",
"Qiq": "222-222-2222",
"Rob": "333-333-3333",
"Stu": "444-444-4444",
"Tad": "555-555-5555",
"Uwe": "111-111-1111",
"Val": "222-222-2222",
"Wil": "333-333-3333",
"Xiu": "444-444-4444",
"Yam": "555-555-5555",
"Zed": "111-111-1111"
};

function phoneBookLookUp (phonebook, number) {
for (let name in phonebook) {
if (phonebook[name] == number) {
console.log(name)
}
}
}

console.log(phoneBookLookUp(phoneBook, "333-333-3333"));
23 changes: 23 additions & 0 deletions js/reverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const inputString = 'building';

function reverseString(num) {
// Iteratively concatenate a result string with the character popped off at the end of the given string
let result = "";
let tmp = num.split('');
while (tmp.length) {
result += tmp.pop();
}
return result;
}

function reverseStringAlt(num) {
// Can't decide if this implementation would be faster. What happens if we want to reverse an entire novel? :D
let result = "";
for (let i = num.length - 1; i >= 0; i--) {
result += num.charAt(i);
}
return result
}

console.log(reverseString(inputString));
console.log(reverseStringAlt(inputString));