From 114957d732232afb4e7b45f2b14012b99f44b558 Mon Sep 17 00:00:00 2001 From: Eric Washburn Date: Thu, 11 Jan 2018 16:25:08 -0500 Subject: [PATCH 1/2] eric homework up to 8, 9 unfinished --- problems/problem1.js | 19 ++++++++++++------- problems/problem2.js | 10 +++++++--- problems/problem3.js | 31 ++++++++++++++++++++++++------- problems/problem4.js | 19 ++++++++++++++++--- problems/problem5.js | 24 ++++++++++++++++++++---- problems/problem6.js | 31 +++++++++++++++++++++++++++---- problems/problem7.js | 30 +++++++++++++++++++++++++++--- problems/problem8.js | 19 ++++++++++++++++--- problems/problem9.js | 26 ++++++++++++++++++++++++-- 9 files changed, 173 insertions(+), 36 deletions(-) diff --git a/problems/problem1.js b/problems/problem1.js index 6d7505c..190bc64 100644 --- a/problems/problem1.js +++ b/problems/problem1.js @@ -2,22 +2,26 @@ var assert = require('assert'); // we need 5 test cases. I provided 1 input let inputs = [ - "" + "", "john", "1a2s3d", "23", "Telephone" ] let outputs = [ - + undefined, "j", "1", "2", "T" ] -// 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 + +// Make this function return the first character of the string that is passed to it. If the string does not have a first letter, return undefined function f(str) { - +if(str.length === 0) { + return undefined; +} +return str[0]; } function runTest(i) { - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); } runTest(0); @@ -25,3 +29,4 @@ runTest(1); runTest(2); runTest(3); runTest(4); + diff --git a/problems/problem2.js b/problems/problem2.js index d54ae74..baf1e15 100644 --- a/problems/problem2.js +++ b/problems/problem2.js @@ -2,16 +2,20 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "", "john", "1a2s3d", "23", "Telephone" ] let outputs = [ - + undefined, "n", "d", "3", "e" ] // 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) { - + if (str.length === 0) { + return undefined; + } else { + return (str[str.length-1]); + } } function runTest(i) { diff --git a/problems/problem3.js b/problems/problem3.js index 11358c6..dbb9f86 100644 --- a/problems/problem3.js +++ b/problems/problem3.js @@ -3,25 +3,42 @@ var assert = require('assert'); // we need 7 test cases. I've provided 2. let inputs = [ [2, 4], - [-3, 3] + [-3, 3], + [1, 2], + [0, 0], + ["a",8], + [0,"r"], + [1, 2] ] let outputs = [ 6, - 0 + 0, + 3, + 0, + undefined, + undefined, + 3 ] /* 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 f(x) { + var results = x[0] + x[1]; + + if (isNaN(results)) { + return undefined; + } else { + return results; + } } + function runTest(i) { - var expected = outputs[i]; - var actual = f(inputs[i]); - assert.deepEqual(actual, expected); + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); } runTest(0); diff --git a/problems/problem4.js b/problems/problem4.js index 4082082..a3d5f0e 100644 --- a/problems/problem4.js +++ b/problems/problem4.js @@ -3,12 +3,25 @@ var assert = require('assert'); // we need 8 test cases. I've provided the first 2 let inputs = [ ["hello", 4], - ["", 2] + ["", 2], + ["john", 3], + ["john", 3], + ["john", 3], + ["john", 3], + ["john", 3], + ["bohn", 3] ] let outputs = [ "o", - undefined + undefined, + "n", + "n", + "n", + "n", + "n", + "n" + ] /* @@ -21,7 +34,7 @@ f("abc", 0); // a */ function f(str, index) { - + return str[index]; } function runTest(i) { diff --git a/problems/problem5.js b/problems/problem5.js index b1e2e44..84f766c 100644 --- a/problems/problem5.js +++ b/problems/problem5.js @@ -2,18 +2,34 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - [2, 7] + [2, 7], + [1, 2], + [2, 3], + [3, 4], + [4, 5] ] let outputs = [ - 14 + 14, + 2, + 6, + 12, + 20 ] /* 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 f(arr) { + var x = arr[0]; + var y = arr[1]; + var result = x*y; + if (isNaN(result)) { + return undefined; + } else { + return result; + } + } function runTest(i) { diff --git a/problems/problem6.js b/problems/problem6.js index d31ae17..06f0ab6 100644 --- a/problems/problem6.js +++ b/problems/problem6.js @@ -4,11 +4,20 @@ var assert = require('assert'); // we need 6 test cases. let inputs = [ ["add", 10, 20], - ["chair", 20, 10] + ["chair", 20, 10], + ["mult", 20, 10], + ["sub", 20, 10], + ["add", 20, 10], + ["add", 30, 5] ] let outputs = [ - 30 + 30, + undefined, + 200, + 10, + 30, + 35 ] /* @@ -21,8 +30,22 @@ f("mult", 2, 3); // 6 f("spoof", 10, 10); // undefined */ -function f(operation, firstArgument, secondArgument) { - +function f(operation) { + var operation1 = operation[0]; + var firstArgument1 = operation[1]; + var secondArgument2 = operation[2]; + var add = firstArgument1 + secondArgument2; + var sub = firstArgument1 - secondArgument2; + var mult = firstArgument1 * secondArgument2; + if (operation[0] === "add") { + return add; + } else if ( operation1 === "sub") { + return sub; + } else if ( operation1 === "mult") { + return mult; + } else { + return undefined; + } } function runTest(i) { diff --git a/problems/problem7.js b/problems/problem7.js index c3bf4b1..4323b2a 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -2,11 +2,23 @@ var assert = require('assert'); // we need 7 test cases. let inputs = [ - + ["foo", 3], + ["foo", 4], + ["foo", 5], + ["foo", 6], + ["foo", 7], + ["foo", "er"], + ["foo", -1] ] let outputs = [ - + "foofoofoo", + "foofoofoofoo", + "foofoofoofoofoo", + "foofoofoofoofoofoo", + "foofoofoofoofoofoofoo", + undefined, + "", ] /* @@ -19,7 +31,19 @@ f("foo", 3) // "foofoofoo" f("fo", 3) // "fofofo" f("foo", -1) // undefined */ -function f(str, n) { +function f(str) { + var word = str[0]; + var mult = str[1]; + var emptyString = ""; + if (typeof word !== 'string') { + return undefined; + } else if (isNaN(mult)) { + return undefined; + } else if (mult >= 0) { + return word.repeat(mult); + } else { + return emptyString; + } } diff --git a/problems/problem8.js b/problems/problem8.js index 6165932..5d40551 100644 --- a/problems/problem8.js +++ b/problems/problem8.js @@ -2,19 +2,32 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ + "hello", + "what are", + "find", + "the", + "last" ] let outputs = [ - + "olleh", + "era tahw", + "dnif", + "eht", + "tsal" ] /* 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 f(x) { + var reversed = "" + for (var i = x.length-1; i >= 0; i--) { + reversed += x[i]; + } + return reversed; } function runTest(i) { diff --git a/problems/problem9.js b/problems/problem9.js index 5c52ef5..48c6864 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -2,11 +2,20 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "one seven", + "two five", + "three two", + "", + "nineteen" ] let outputs = [ - + "one", + "two", + "three", + "", + "nineteen" + ] /* @@ -14,7 +23,20 @@ Make this function return the longest word in the input string. If the input str If multiple words have the same length, return the last one that matches. */ function f(str) { + var longestWord = ''; + var longest = 0; + //slit sentence up into an array + var splitString = str.split(" "); +console.log(splitString); + for (var i = 0; i < splitString.length; i++) { + if (longest > splitString[i].length) { + longest = splitString[i].length; + longestWord = splitString[i]; + return longestWord; + } + } + //find length of each item } function runTest(i) { From 76dc5ac268478784235186ffc488a941084680c6 Mon Sep 17 00:00:00 2001 From: Eric Washburn Date: Mon, 15 Jan 2018 10:14:30 -0500 Subject: [PATCH 2/2] first commit --- README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b356aa --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# EricWashburn