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) { 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) { 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) { 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) { 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) { 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) { 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) { 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) { 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/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); diff --git a/problems/problem6.js b/problems/problem6.js index d31ae17..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 ] /* @@ -16,13 +25,23 @@ 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) { + 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) { @@ -37,4 +56,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..8492396 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -2,27 +2,48 @@ 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 ] /* 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) { + 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); + } + 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]; @@ -36,5 +57,4 @@ runTest(2); runTest(3); runTest(4); runTest(5); -runTest(6); - +runTest(6); \ No newline at end of file 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) { 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) {