diff --git a/js/filterLongWords.js b/js/filterLongWords.js index e69de29..841aeb8 100644 --- a/js/filterLongWords.js +++ b/js/filterLongWords.js @@ -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); diff --git a/js/fizzbuzz.js b/js/fizzbuzz.js index e69de29..df2343e 100644 --- a/js/fizzbuzz.js +++ b/js/fizzbuzz.js @@ -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> 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); \ No newline at end of file diff --git a/js/grade.js b/js/grade.js index e69de29..b3991fe 100644 --- a/js/grade.js +++ b/js/grade.js @@ -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) + "."); diff --git a/js/phonebook.js b/js/phonebook.js index e69de29..0414af5 100644 --- a/js/phonebook.js +++ b/js/phonebook.js @@ -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")); diff --git a/js/reverse.js b/js/reverse.js index e69de29..5b0163a 100644 --- a/js/reverse.js +++ b/js/reverse.js @@ -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));