From dcb50262e6520d1458278114553050beac982aab Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 15:56:29 -0500 Subject: [PATCH 01/14] Problem 1 Done --- problems/problem1.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/problems/problem1.js b/problems/problem1.js index 6d7505c..63ed39a 100644 --- a/problems/problem1.js +++ b/problems/problem1.js @@ -2,16 +2,26 @@ var assert = require('assert'); // we need 5 test cases. I provided 1 input let inputs = [ - "" + "", + "abc", + "12345", + "hello", + "This is a string" ] let outputs = [ - + undefined, + "a", + "1", + "h", + "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 function f(str) { - + if (str.length > 0){ + return str.charAt(0); + }; } function runTest(i) { From 086e452c1d967262fb550fdee5c1becccfcd58da Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 16:23:49 -0500 Subject: [PATCH 02/14] Problem 2 Done --- problems/problem2.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/problems/problem2.js b/problems/problem2.js index d54ae74..2238b97 100644 --- a/problems/problem2.js +++ b/problems/problem2.js @@ -2,16 +2,26 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "", + "abc", + "12345", + "hello", + "This is a string" ] - -let outputs = [ +let outputs = [ + undefined, + "c", + "5", + "o", + "g" ] // 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 str.charAt(str.length - 1); + } } function runTest(i) { From 80fb83c5123586cf1dfcdbf5667c4aa9fadc4d3e Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 17:20:36 -0500 Subject: [PATCH 03/14] Problem 3 Done --- problems/problem3.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/problems/problem3.js b/problems/problem3.js index 11358c6..c29deff 100644 --- a/problems/problem3.js +++ b/problems/problem3.js @@ -3,19 +3,32 @@ var assert = require('assert'); // we need 7 test cases. I've provided 2. let inputs = [ [2, 4], - [-3, 3] + [-3, 3], + ["a", 8], + ["x", "z"], + [21, 21], + [3, 39], + [8, 5] ] let outputs = [ 6, - 0 + 0, + undefined, + undefined, + 42, + 42, + 13 ] /* 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) { + console.log(x) + if (!isNaN(x[0]) & !isNaN(x[1])) { + return x[0] + x[1]; + } } function runTest(i) { From dd2eb31f68ff7ef7f4c71e0fbce638b648a0721b Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 18:14:33 -0500 Subject: [PATCH 04/14] Problem 4 Done --- problems/problem4.js | 30 +++++++++++++++++++++--------- problems/problem6.js | 11 +++++------ problems/problem7.js | 13 +++++-------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/problems/problem4.js b/problems/problem4.js index 4082082..6c79397 100644 --- a/problems/problem4.js +++ b/problems/problem4.js @@ -3,25 +3,37 @@ var assert = require('assert'); // we need 8 test cases. I've provided the first 2 let inputs = [ ["hello", 4], - ["", 2] + ["", 2], + ["Hi", 0], + ["Lorem Ipsum", 3], + ["1orem", 0], + ["Lorem lipsum", 6], + ["20346127", 1], + ["World", 0] ] let outputs = [ "o", - undefined + undefined, + "H", + "e", + "1", + "l", + "0", + "W" ] /* 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 - +f(["hello", 1]); // e +f(["", 4]); // undefined +f(["abc", 0]); // a */ function f(str, index) { - + if (str.length > 0 && str.length > index ){ + return str.charAt(index); + }; } function runTest(i) { @@ -38,4 +50,4 @@ runTest(3); runTest(4); runTest(5); runTest(6); -runTest(7); +runTest(7); \ No newline at end of file diff --git a/problems/problem6.js b/problems/problem6.js index d31ae17..0907da2 100644 --- a/problems/problem6.js +++ b/problems/problem6.js @@ -16,12 +16,11 @@ 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 - +f(["add", 10, 20]); // 30 +f(["mult", 2, 3]); // 6 +f(["spoof", 10, 10]); // undefined */ -function f(operation, firstArgument, secondArgument) { +function f(arr) { } @@ -37,4 +36,4 @@ runTest(1); runTest(2); runTest(3); runTest(4); -runTest(5); +runTest(5); \ No newline at end of file diff --git a/problems/problem7.js b/problems/problem7.js index c3bf4b1..df72ed9 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -12,14 +12,12 @@ 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 +f(["foo", 3]) // "foofoofoo" +f(["fo", 3]) // "fofofo" +f(["foo", -1]) // undefined */ -function f(str, n) { +function f(arr) { } @@ -36,5 +34,4 @@ runTest(2); runTest(3); runTest(4); runTest(5); -runTest(6); - +runTest(6); \ No newline at end of file From 4662ea43a0dfcd23ce9f3f5a1a0349b957e225cf Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 18:36:07 -0500 Subject: [PATCH 05/14] Problem 5 Done --- problems/problem5.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/problems/problem5.js b/problems/problem5.js index b1e2e44..33794ad 100644 --- a/problems/problem5.js +++ b/problems/problem5.js @@ -2,25 +2,35 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - [2, 7] + [2, 7], + [2, 21], + [3, 4], + ["Something", 3], + ["A", "b"] ] let outputs = [ - 14 + 14, + 42, + 12, + 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. */ -function f(x, y) { - +function f(x) { + if (!isNaN(x[0]) & !isNaN(x[1])) { + return x[0] * x[1]; + } } 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); + 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); From 3041c75b56adbc915d8cced44f6b604986bc14d6 Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 18:54:18 -0500 Subject: [PATCH 06/14] Problem 6 Done --- problems/problem6.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/problems/problem6.js b/problems/problem6.js index 0907da2..21802ca 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", 10, 10], + ["1234567", 2, 2], + ["add", 2, 2], + ["sub", 44, 2] ] let outputs = [ - 30 + 30, + undefined, + 100, + undefined, + 4, + 42 ] /* @@ -21,7 +30,18 @@ f(["mult", 2, 3]); // 6 f(["spoof", 10, 10]); // undefined */ function f(arr) { - + var task = arr[0]; + var x = arr[1]; + var y = arr[2]; + if (task == "add" ) { + return x + y; + } + else if (task == "sub") { + return x - y; + } + else if (task == "mult") { + return x * y; + } } function runTest(i) { From d9fb596743f0a3f26016811bc575cf15b62cd3f1 Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 20:13:11 -0500 Subject: [PATCH 07/14] Problem 7 Done --- problems/problem7.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/problems/problem7.js b/problems/problem7.js index df72ed9..d4445c2 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -2,11 +2,23 @@ var assert = require('assert'); // we need 7 test cases. let inputs = [ - + ["to", 2], + ["bon", 2], + ["tic ", 4], + ["Zero", 0], + ["Negative", -3], + ["", 6], + ["Hello", "World"] ] let outputs = [ - + "toto", + "bonbon", + "tic tic tic tic ", + "", + "", + "", + undefined ] /* @@ -18,9 +30,21 @@ f(["fo", 3]) // "fofofo" f(["foo", -1]) // undefined */ function f(arr) { - + var str = arr[0]; + var num = arr[1]; + if (typeof str == 'string' && typeof num == 'number') { + if (str.length > 0 && num > 0) { + return str.repeat(num); + console.log("--------" + arr + "Worked"); + } + else { + return ""; + } + } } +f(["foo", 3]) + function runTest(i) { if(i > inputs.length) throw new Error("You do not have enough test cases"); var expected = outputs[i]; From 3e2d8e3b58c7570b42044eb2cf18eeea63462de0 Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 20:51:55 -0500 Subject: [PATCH 08/14] Problem 8 Done --- problems/problem7.js | 1 - problems/problem8.js | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/problems/problem7.js b/problems/problem7.js index d4445c2..8492396 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -35,7 +35,6 @@ function f(arr) { if (typeof str == 'string' && typeof num == 'number') { if (str.length > 0 && num > 0) { return str.repeat(num); - console.log("--------" + arr + "Worked"); } else { return ""; diff --git a/problems/problem8.js b/problems/problem8.js index 6165932..a5f7317 100644 --- a/problems/problem8.js +++ b/problems/problem8.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "hello", + "how are you", + "Lorem ipsum", + "yoyo", + "kayak" ] let outputs = [ - + "olleh", + "uoy era woh", + "muspi meroL", + "oyoy", + "kayak" ] /* @@ -14,7 +22,12 @@ Make this function return the input string, reversed. For example "hello" would You must use a for loop for this exercise. */ function f(str) { - + var answer = []; + for (var i = 0; i < str.length; i++) { + var char = str.charAt(i); + answer.unshift(char); + } + return answer.join(""); } function runTest(i) { From 24bd81aecccdcb602902a3165c1383046e25c7e7 Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 21:19:05 -0500 Subject: [PATCH 09/14] Problem 9 Done --- problems/problem9.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/problems/problem9.js b/problems/problem9.js index 5c52ef5..b7fd1a5 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "Lorem ipsum si meliora dies", + "Alpha beta delta", + 12345, + "", + "111 222 333" ] let outputs = [ - + "meliora", + "delta", + undefined, + "", + "333" ] /* @@ -14,7 +22,18 @@ 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) { - + if (typeof str == 'string') { + var words = str.split(" "); + var longest = ""; + + var findLongest = function(word){ + if (word.length >= longest.length) { + longest = word; + } + } + words.forEach(findLongest); + } + return longest; } function runTest(i) { From b337908de2005d65d81d88dfcc0f4ea8fc1f7e03 Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 22:02:33 -0500 Subject: [PATCH 10/14] Problem 10 Done --- problems/problem10.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/problems/problem10.js b/problems/problem10.js index e7eddde..d47cc60 100644 --- a/problems/problem10.js +++ b/problems/problem10.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "hey listen", + "ALL YOUR BASE ARE BELONG TO US", + "the cake is a lie", + "sTAY a wHILE aND lISTEN", + "FOR THE EMPEROR" ] let outputs = [ - + "Hey Listen", + "All Your Base Are Belong To Us", + "The Cake Is A Lie", + "Stay A While And Listen", + "For The Emperor" ] /* @@ -17,7 +25,16 @@ f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong */ function f(str) { - + var words = str.split(" "); + var answer = []; + + for (var i = 0; i < words.length; i++) { + var capitalizedWord = words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase(); + answer.push(capitalizedWord); + } + + return answer.join(" "); + } function runTest(i) { From 4f6f0a2fe96c4cf35de3c0a7751abccb20fa9ffd Mon Sep 17 00:00:00 2001 From: Kamylle Date: Wed, 10 Jan 2018 22:10:48 -0500 Subject: [PATCH 11/14] Problem 11 Done --- problems/problem11.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/problems/problem11.js b/problems/problem11.js index a9db8bc..7897a53 100644 --- a/problems/problem11.js +++ b/problems/problem11.js @@ -2,18 +2,33 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + [1, 2, 3, 4, 5], + [40, 2], + ["a", 13], + ["x", "y"], + [] ] let outputs = [ - + 15, + 42, + 13, + 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. */ function f(arr) { - + var answer = 0; + for (var i = 0; i < arr.length; i++) { + if (typeof arr[i] == 'number') { + answer = answer + arr[i]; + } + } + + return answer; } function runTest(i) { From 2922d65dfc96e4d1c0d39fa58c281ffde33c7882 Mon Sep 17 00:00:00 2001 From: Kamylle Date: Thu, 11 Jan 2018 09:35:31 -0500 Subject: [PATCH 12/14] Problem 13 Done --- problems/problem13.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/problems/problem13.js b/problems/problem13.js index 90669e3..32ec430 100644 --- a/problems/problem13.js +++ b/problems/problem13.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "kayak", + "radar", + "notakayak", + "hello", + "worldlrow" ] let outputs = [ - + true, + true, + false, + false, + true ] /* @@ -16,7 +24,16 @@ RADAR -> Yes JAVASCRIPT -> No */ function f(str) { - + var arr = str.split(""); + var rev = arr.reverse(""); + var ans = rev.join(""); + + if (str == ans) { + return true; + } + else { + return false + } } function runTest(i) { From ab0d1532d107238145940443c93f3c766b7e1da1 Mon Sep 17 00:00:00 2001 From: Kamylle Date: Thu, 11 Jan 2018 17:53:58 -0500 Subject: [PATCH 13/14] Problem 12 Done --- problems/problem12.js | 46 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/problems/problem12.js b/problems/problem12.js index 2947d1b..98752e8 100644 --- a/problems/problem12.js +++ b/problems/problem12.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + [[0,1,2,3], [1,3,4,5]], + [[1,2,3], [1,2,3]], + [[1,2,42], [1,2,13]], + [[1], [1]], + ["a"] ] let outputs = [ - + [0,2,4,5], + [], + [42,13], + [], + undefined ] /* @@ -19,8 +27,40 @@ 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 isPartOfArray(x, list){ + for (var i = 0; i < list.length; i++) { + if (x == list[i]) { + return true; + } + } + return false; +} + +function f(arr) { + var firstArray = arr[0]; + var secondArray = arr[1]; + var answer = []; + + if (Array.isArray(arr) && Array.isArray(firstArray) && Array.isArray(secondArray)) { + for (var i = 0; i < firstArray.length; i++) { + if (!isPartOfArray(firstArray[i], secondArray) + && !isPartOfArray(firstArray[i], answer)) { + answer.push(firstArray[i]); + } + } + + for (var i = 0; i < secondArray.length; i++) { + if (!isPartOfArray(secondArray[i], firstArray) + && !isPartOfArray(secondArray[i], answer)) { + answer.push(secondArray[i]); + } + } + + return answer; + } + } function runTest(i) { From c3018d66c72da2cbc487f19096c5bac529ff858f Mon Sep 17 00:00:00 2001 From: Kamylle Date: Thu, 11 Jan 2018 23:10:24 -0500 Subject: [PATCH 14/14] Problem 14 Done --- problems/problem14.js | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/problems/problem14.js b/problems/problem14.js index eb49b73..db6844c 100644 --- a/problems/problem14.js +++ b/problems/problem14.js @@ -2,11 +2,28 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam", + "Handshake backing conversion android traction angel investor analytics low hanging fruit ecosystem", + "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", + "0123456789012345678901234567890123456789", + "This string is too short." ] +/*To make sure I had the right position of \n in the expected output*/ +/*var z = "Handshake backing conversion android traction angel investor analytics low hanging fruit ecosystem"; +console.log("39eme = " + z.charAt(38)); +console.log("40eme = " + z.charAt(39)); +console.log("41eme = " + z.charAt(40)); +console.log("79eme = " + z.charAt(78)); +console.log("80eme = " + z.charAt(79)); +console.log("81eme = " + z.charAt(80)); */ + let outputs = [ - + "Lorem ipsumos dolor sit amet consectetur\nadipisicing elit. Magni quisquam", + "Handshake backing conversion android tra\nction angel investor analytics low hangi\nng fruit ecosystem", + "a b c d e f g h i j k l m n o p q r s t \nu v w x y z", + "0123456789012345678901234567890123456789", + "This string is too short." ] /* @@ -31,7 +48,19 @@ Lorem ipsumos dolor sit amet consectetur even though there is a space before the a in adipisicing */ function f(str) { - + var myString = str; + ans = []; + while (myString.length > 40) { + ans.push(myString.slice(0,40) + "\n"); + myString = myString.slice(40); + + if (myString.charAt(0) == " ") { + myString = myString.slice(1); + } + } + + ans.push(myString); + return ans.join(""); } function runTest(i) {