From 51f69cebddfcaf8ace241f4dea0b6511353e40ae Mon Sep 17 00:00:00 2001 From: Anouk Date: Thu, 11 Jan 2018 07:37:14 -0500 Subject: [PATCH] Add files via upload Assertion Testing JS Problems 1-14 - Solved --- problems/problem1.js | 61 ++++++++++++---------- problems/problem10.js | 95 +++++++++++++++++++++------------- problems/problem11.js | 74 ++++++++++++++++----------- problems/problem12.js | 81 +++++++++++++++-------------- problems/problem13.js | 74 ++++++++++++++------------- problems/problem14.js | 116 ++++++++++++++++++++++++------------------ problems/problem2.js | 61 ++++++++++++---------- problems/problem3.js | 76 +++++++++++++++------------ problems/problem4.js | 94 +++++++++++++++++++--------------- problems/problem5.js | 72 +++++++++++++++----------- problems/problem6.js | 107 +++++++++++++++++++++++--------------- problems/problem7.js | 92 ++++++++++++++++++--------------- problems/problem8.js | 88 ++++++++++++++++++++------------ problems/problem9.js | 80 +++++++++++++++++------------ 14 files changed, 682 insertions(+), 489 deletions(-) diff --git a/problems/problem1.js b/problems/problem1.js index 6d7505c..6a618b0 100644 --- a/problems/problem1.js +++ b/problems/problem1.js @@ -1,27 +1,34 @@ -var assert = require('assert'); - -// we need 5 test cases. I provided 1 input -let inputs = [ - "" -] - -let outputs = [ - -] - -// Make this function return the first letter of the string that is passed to it. If the string does not have a first letter, return undefined -function f(str) { - -} - -function runTest(i) { - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); +var assert = require('assert'); + +// we need 5 test cases. I provided 1 input +let inputs = [ + "", + "abc", + "123", + "'&?", + {} +] + +let outputs = [ + undefined, + "a", + "1", + "'", + undefined +] + +// Make this function return the first letter of the string that is passed to it. +// If the string does not have a first letter, return undefined. +const f = str => str.length > 0 ? str[0] : undefined; + +function runTest(i) { + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); \ No newline at end of file diff --git a/problems/problem10.js b/problems/problem10.js index e7eddde..b6f1005 100644 --- a/problems/problem10.js +++ b/problems/problem10.js @@ -1,35 +1,60 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -Make this function return the input string, capitalized. You must use a for loop. For example: - -f("hello world"); // Hello World -f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong - -*/ -function f(str) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "hello WORLD", + "YOU have NO chance TO survive MAKE your TIME", + {}, + [], + 0 +] + +let outputs = [ + "Hello World", + "You Have No Chance To Survive Make Your Time", + undefined, + undefined, + undefined +] + +/* +Make this function return the input string, capitalized. +You must use a for loop. For example: + +f("hello world"); // Hello World +f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong + +*/ + +// Gotta use a for loop... again. :( + +const f = str => { + if (typeof str === "string") { + if (str.includes(" ")) { + str = str.toLowerCase().split(" "); // 'str' now "cast" (sic) as an array. + const resArr = []; + for (i = 0; i < str.length; i++) { + resArr.push(str[i][0].toUpperCase() + str[i].slice(1, str[i].length)); + } + return resArr.join(" "); + } else { + return str.length > 0 ? str[0].toUpperCase() + str.slice(1, str.length - 1).toLowerCase() : "" ; + } + } else { + return undefined; + } +} + +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); + diff --git a/problems/problem11.js b/problems/problem11.js index a9db8bc..1b8acf0 100644 --- a/problems/problem11.js +++ b/problems/problem11.js @@ -1,31 +1,43 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -Make this function return the sum of all the numbers in the input array. If any element in the array is not a number, skip it. If the array is empty, return zero. -*/ -function f(arr) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + [1, 5, 3, 7, 9, "c", {}, [], 24, 1], + [], + ["a", 2, null, "c", 0, [], 45, 16, 0], + ["a", "b", "c", undefined, null, {}, [], "z"], + {} +] + +let outputs = [ + 50, + 0, + 63, + 0, + 0 +] + +/* +Make this function return the sum of all the numbers in the input array. +If any element in the array is not a number, skip it. +If the array is empty, return zero. +*/ + +// No mention of other input types having to return "undefined"; so returning "0" in those instances, too (as seen in the last test case). + +const f = arr => { + return Array.isArray(arr) || arr.length > 0 ? arr.reduce((t, c) => { return typeof c !== "number" ? t + 0 : t + c }, 0) : 0; +} + +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); \ No newline at end of file diff --git a/problems/problem12.js b/problems/problem12.js index 2947d1b..17023a4 100644 --- a/problems/problem12.js +++ b/problems/problem12.js @@ -1,38 +1,43 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -Make this function return the elements that are unique to array1 and array2. -If there are no unique elements return an empty array. -If the inputs are anything other than arrays, return undefined. -For example: - -uniqueElements([0,1,2,3], [1,3,4,5]); // [0,4,5] -uniqueElements([1,2,3], [1,2,3]); // [] -uniqueElements(2,3); // undefined, not arrays -*/ -function f(arr1, arr2) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + [[0, 1, 2, 3], [1, 3, 4, 5]], + ["hello", []], + [{}], + [(2, 3)], + [1, []] +] + +let outputs = [ + [0, 2, 4, 5], + undefined, + undefined, + undefined, + undefined +] + +/* +Make this function return the elements that are unique to array1 and array2. +If there are no unique elements return an empty array. +If the inputs are anything other than arrays, return undefined. +For example: + +uniqueElements([0,1,2,3], [1,3,4,5]); // [0,4,5] +uniqueElements([1,2,3], [1,2,3]); // [] +uniqueElements(2,3); // undefined, not arrays +*/ +const f = (arr1, arr2) => !Array.isArray(arr1) || !Array.isArray(arr2) ? undefined : arr1.concat(arr2).filter(e => arr1.includes(e) && !arr2.includes(e) || !arr1.includes(e) && arr2.includes(e)); + +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + var actual = f(...inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); \ No newline at end of file diff --git a/problems/problem13.js b/problems/problem13.js index 90669e3..bb66692 100644 --- a/problems/problem13.js +++ b/problems/problem13.js @@ -1,34 +1,40 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -Make this function return true if the input string is a palindrome, and false otherwise. A palindrome is simply a string that is the same if you reverse it. - -RADAR -> Yes -JAVASCRIPT -> No -*/ -function f(str) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "A Santa Lived As a Devil At NASA", + "Laravel", + 0, + {}, + [] +] + +let outputs = [ + true, + false, + undefined, + undefined, + undefined +] + +/* +Make this function return true if the input string is a palindrome, and false otherwise. +A palindrome is simply a string that is the same if you reverse it. + +RADAR -> Yes +JAVASCRIPT -> No +*/ +const f = str => typeof str === 'string' ? str.replace(/\s/g, '').toLowerCase() === str.replace(/\s/g, '').toLowerCase().split('').reverse().join('') : undefined; + +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); \ No newline at end of file diff --git a/problems/problem14.js b/problems/problem14.js index eb49b73..97338cb 100644 --- a/problems/problem14.js +++ b/problems/problem14.js @@ -1,49 +1,67 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -Make this function return the input string wrapped to 40 characters per line. -This means you'll have to insert a newline \n character after every 40 characters in the input string. -If the next character after a cut is a space, then do not display it. - -For example with the input: - -Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam - -the output would be: - -Lorem ipsumos dolor sit amet consectetur -adipisicing elit. Magni quisquam - -instead of: - -Lorem ipsumos dolor sit amet consectetur - adipisicing elit. Magni quisquam - - even though there is a space before the a in adipisicing -*/ -function f(str) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla rutrum elit in vehicula tempus. Suspendisse iaculis, sapien aliquam rhoncus ornare, tortor mauris molestie sapien.", + 0, + {}, + [], + null +] + +let outputs = [ + "Lorem ipsumos dolor sit amet consectetur\nadipisicing elit. Magni quisquam\n", + "Lorem ipsum dolor sit amet, consectetur\nadipiscing elit. Nulla rutrum elit in ve\nhicula tempus. Suspendisse iaculis, sapi\nen aliquam rhoncus ornare, tortor mauris\nmolestie sapien.\n", + undefined, + undefined, + undefined, + undefined +] + +/* +Make this function return the input string wrapped to 40 characters per line. +This means you'll have to insert a newline \n character after every 40 characters in the input string. +If the next character after a cut is a space, then do not display it. + +For example with the input: + +Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam + +the output would be: + +Lorem ipsumos dolor sit amet consectetur +adipisicing elit. Magni quisquam + +instead of: + +Lorem ipsumos dolor sit amet consectetur + adipisicing elit. Magni quisquam + + even though there is a space before the a in adipisicing +*/ + +// not as robust as I'd like, due to the trimming... +// as a result of the trim, some characters are cut at char 39 instead of 40. + +const f = str => { + if (typeof str !== "string") { return undefined } + const strArr = []; + for (i = 0, j = 1; i < str.length; i += 40, j++) { + strArr.push((str.slice(i, j * 40).trim())+"\n"); + } + return strArr.join(''); +} + +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); \ No newline at end of file diff --git a/problems/problem2.js b/problems/problem2.js index d54ae74..d8d979c 100644 --- a/problems/problem2.js +++ b/problems/problem2.js @@ -1,27 +1,34 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -// Make this function return the last letter of the string that is passed to it. If the string does not have a last letter, return undefined -function f(str) { - -} - -function runTest(i) { - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "", + "abc", + "123", + "'?$", + {}, +] + +let outputs = [ + undefined, + "c", + "3", + "$", + undefined +] + +// Make this function return the last letter of the string that is passed to it. +// If the string does not have a last letter, return undefined +const f = str => str.length > 0 ? str[str.length-1] : undefined; + +function runTest(i) { + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); diff --git a/problems/problem3.js b/problems/problem3.js index 11358c6..d087fb3 100644 --- a/problems/problem3.js +++ b/problems/problem3.js @@ -1,33 +1,43 @@ -var assert = require('assert'); - -// we need 7 test cases. I've provided 2. -let inputs = [ - [2, 4], - [-3, 3] -] - -let outputs = [ - 6, - 0 -] - -/* -Make this function return the sum of the two numbers that are passed to it. If one of the numbers is not passed, or if anything other than numbers are passed, return undefined. -*/ -function f(x, y) { - -} - -function runTest(i) { - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); -runTest(5); -runTest(6); +var assert = require('assert'); + +// we need 7 test cases. I've provided 2. +let inputs = [ + [2, 4], + [-3, 3], + ["a", -5], + [0, 0], + [3, {}], + [4, ["3"]], + [] +] + +let outputs = [ + 6, + 0, + undefined, + 0, + undefined, + undefined, + undefined +] + +/* +Make this function return the sum of the two numbers that are passed to it. +If one of the numbers is not passed, or if anything other than numbers are passed, return undefined. +*/ +const f = (x, y) => typeof x === "number" && typeof y === "number" ? x + y : undefined; + +function runTest(i) { + var expected = outputs[i]; + // originally : var actual = f(inputs[i]) -- spread operator was missing; + var actual = f(...inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); +runTest(5); +runTest(6); \ No newline at end of file diff --git a/problems/problem4.js b/problems/problem4.js index d30a00c..559c4df 100644 --- a/problems/problem4.js +++ b/problems/problem4.js @@ -1,41 +1,53 @@ -var assert = require('assert'); - -// we need 8 test cases. I've provided the first 2 -let inputs = [ - ["hello", 4], - ["", 2] -] - -let outputs = [ - "o", - undefined -] - -/* -Make this function return the letter at the specified position in the string. If no such letter exists, it should return undefined. - -For example: -f(["hello", 1]); // e -f(["", 4]); // undefined -f(["abc", 0]); // a - -*/ -function f(arr) { - -} - -function runTest(i) { - var expected = outputs[i]; - var input = inputs[i]; - var actual = f(input[0], input[1]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); -runTest(5); -runTest(6); -runTest(7); +var assert = require('assert'); + +// we need 8 test cases. I've provided the first 2 +let inputs = [ + ["hello", 4], + ["", 2], + [{}, 3], + ["foobar","4"], + [4], + [3, 3], + [], + ["final", 9] +] + +let outputs = [ + "o", + undefined, + undefined, + undefined, + undefined, + undefined, + undefined, + undefined +] + +/* +Make this function return the letter at the specified position in the string. +If no such letter exists, it should return undefined. + +For example: +f("hello", 1); // e +f("", 4); // undefined +f("abc", 0); // a + +*/ + +const f = (str, index) => typeof str === "string" && typeof index === "number" && index < str.length ? str[index] : undefined; + +function runTest(i) { + var expected = outputs[i]; + var input = inputs[i]; + var actual = f(input[0], input[1]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); +runTest(5); +runTest(6); +runTest(7); diff --git a/problems/problem5.js b/problems/problem5.js index b1e2e44..c5246e5 100644 --- a/problems/problem5.js +++ b/problems/problem5.js @@ -1,30 +1,42 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - [2, 7] -] - -let outputs = [ - 14 -] - -/* -Make this function return the product of the two numbers that are passed to it. If one of the numbers is not passed, or if anything other than numbers are passed, return undefined. -*/ -function f(x, y) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + [2, 7], + [-6,-3], + [0, -5], + [5], + {} +] + +let outputs = [ + 14, + 18, + 0, + undefined, + undefined +] + +/* +Make this function return the product of the two numbers that are passed to it. +If one of the numbers is not passed, or if anything other than numbers are passed, return undefined. +*/ + +const f = (x, y) => typeof x === 'number' && typeof y === 'number' ? x * y : undefined; + +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + // Fixed Assertion Args (next 2 lines) : + var input = inputs[i]; + var actual = f(input[0], input[1]); + // Previous Assertion Args (which were not originally functional) : + // var actual = f(inputs[i]); */ + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); diff --git a/problems/problem6.js b/problems/problem6.js index f74431a..c031e04 100644 --- a/problems/problem6.js +++ b/problems/problem6.js @@ -1,40 +1,67 @@ -// pro tip: use nodemon instead of node -var assert = require('assert'); - -// we need 6 test cases. -let inputs = [ - ["add", 10, 20], - ["chair", 20, 10] -] - -let outputs = [ - 30 -] - -/* -Use the operation argument to decide what this function will return. -If it's "add", return the sum of the two numbers. "sub" return their difference. "mult" return their product. -Anything else return undefined. -For example: -f(["add", 10, 20]); // 30 -f(["mult", 2, 3]); // 6 -f(["spoof", 10, 10]); // undefined - -*/ -function f(arr) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); -runTest(5); +// pro tip: use nodemon instead of node +var assert = require('assert'); + +// we need 6 test cases. +let inputs = [ + ["add", 10, 20], + ["chair", 20, 10], + [{}, 2, 3], + ["mult", "a", 5], + [["sub"],4, 3], + [] +] + +let outputs = [ + 30, + undefined, + undefined, + undefined, + undefined, + undefined +] + +/* +Use the operation argument to decide what this function will return. +If it's "add", return the sum of the two numbers. +"sub" return their difference. +"mult" return their product. +Anything else return undefined. +For example: +f("add", 10, 20); // 30 +f("mult", 2, 3); // 6 +f("spoof", 10, 10); // undefined + +*/ +const f = (operation, firstArgument, secondArgument) => { + if (typeof operation === "string" + && typeof firstArgument === "number" + && typeof secondArgument === "number") { + return operation === "add" ? firstArgument + secondArgument : operation === "sub" + ? firstArgument - secondArgument : operation === "mult" + ? firstArgument * secondArgument : undefined; + } else { + return undefined; + } +} + +/* +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +}*/ + +function runTest(i) { + var expected = outputs[i]; + var input = inputs[i]; + var actual = f(input[0], input[1], input[2]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); +runTest(5); \ No newline at end of file diff --git a/problems/problem7.js b/problems/problem7.js index 8eb18e3..9f6fe8b 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -1,40 +1,52 @@ -var assert = require('assert'); - -// we need 7 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -Make this function return the input string repeated as many times as specified. -If a negative number or zero is specified, return an empty string. If any invalid parameters are supplied return undefined. - -For example: - -f(["foo", 3]) // "foofoofoo" -f(["fo", 3]) // "fofofo" -f(["foo", -1]) // undefined -*/ -function f(arr) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); -runTest(5); -runTest(6); - +var assert = require('assert'); + +// we need 7 test cases. +let inputs = [ + ["yo", 3], + ["", "5"], + [0, 9], + [{}, 4], + ["AA", [3]], + ["1", 0], + [] +] + +let outputs = [ + "yoyoyo", + undefined, + undefined, + undefined, + undefined, + undefined, + undefined +] + +/* +Make this function return the input string repeated as many times as specified. +If a negative number or zero is specified, return an empty string. +If any invalid parameters are supplied return undefined. + +For example: + +f("foo", 3) // "foofoofoo" +f("fo", 3) // "fofofo" +f("foo", -1) // undefined +*/ +const f = (str, n) => typeof str === "string" && typeof n === "number" && n > 0 ? str.repeat(n) : undefined; + +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + // originally : var actual = f(inputs[i]); + var actual = f(...inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); +runTest(5); +runTest(6); + diff --git a/problems/problem8.js b/problems/problem8.js index 6165932..afe6fb9 100644 --- a/problems/problem8.js +++ b/problems/problem8.js @@ -1,32 +1,56 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -Make this function return the input string, reversed. For example "hello" would return "olleh" and "how are you" would return "uoy era woh". -You must use a for loop for this exercise. -*/ -function f(str) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "how are you", + "", + "murder for a jar of red rum", + [], + {} +] + +let outputs = [ + "uoy era woh", + undefined, + "mur der fo raj a rof redrum", + undefined, + undefined +] + +/* +Make this function return the input string, reversed. +For example "hello" would return "olleh" and "how are you" would return "uoy era woh". +You must use a for loop for this exercise. +*/ + +/* More elegant... but no loop. +const f = str => { + return str.split('').reverse().join(''); +} +*/ + +const f = str => { + if (typeof str === "string" && str.length > 0) { + let result = ""; + for (i = str.length-1; i >= 0; i--) { + result += str[i]; + } + return result; + } else { + return undefined; + } +} + +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); + diff --git a/problems/problem9.js b/problems/problem9.js index 5c52ef5..c05d21d 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -1,32 +1,48 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -Make this function return the longest word in the input string. If the input string is empty then return an empty string. -If multiple words have the same length, return the last one that matches. -*/ -function f(str) { - -} - -function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "This is the shortest I can go for", + "", + 0, + "Hello", + "This is going to be funky" // since 'going' (5) is the first longest and 'funky' (5) the last -- the 'sort' function won't return 'funky' by default / without a bit of tweaking. +] + +let outputs = [ + "shortest", + "", + undefined, + "Hello", + "funky" +] + +/* +Make this function return the longest word in the input string. +If the input string is empty then return an empty string. +If multiple words have the same length, return the last one that matches. +*/ + +// Pushing my limits with methods I don't often use & ternary operators. :) +// There was likely a more simple/readable way of doing this. + +const f = str => typeof str !== "string" + ? undefined + : str.includes(" ") + ? str.split(' ').sort((a, b) => a.length === b.length + ? b.length + a.length + : b.length - a.length)[0] + : str; + +function runTest(i) { + if(i > inputs.length) throw new Error("You do not have enough test cases"); + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); \ No newline at end of file