diff --git a/problems/problem1.js b/problems/problem1.js index 6d7505c..bb4698e 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", + "cba", + "123", + "1a2" +]; + let outputs = [ - + undefined, + "a", + "c", + "1", + "1" ] // 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 undefined; + return str[0]; } function runTest(i) { diff --git a/problems/problem10.js b/problems/problem10.js index e7eddde..6128a22 100644 --- a/problems/problem10.js +++ b/problems/problem10.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "hello world", + "ALL YOUR BASE ARE BELONG", + "jAvAscrIpt is FuN", + "ABBBaaach nbxjHH", + "wELComE" ] let outputs = [ - + "Hello World", + "All Your Base Are Belong", + "Javascript Is Fun", + "Abbbaaach Nbxjhh", + "Welcome" ] /* @@ -17,7 +25,12 @@ f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong */ function f(str) { - + var arr = str.toLowerCase().split(" "); + var newStr = ""; + for (var i = 0; i < arr.length; i++) { + arr[i] = arr[i][0].toUpperCase()+arr[i].slice(1); + } + return newStr += arr.join(" "); } function runTest(i) { diff --git a/problems/problem11.js b/problems/problem11.js index a9db8bc..d82aba5 100644 --- a/problems/problem11.js +++ b/problems/problem11.js @@ -2,20 +2,37 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + [2, 7, 10], + [2, 9, 1, 18], + [5, 6, 7], + [-2, 22, -3], + [], ] let outputs = [ - + 19, + 30, + 18, + 17, + 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) { - + if(arr.length === 0) return 0; + var result=0; + for (var i = 0; i < arr.length; i++) { + if(!isNaN(arr[i])) { + result += arr[i] + } + + } + return result; } + function runTest(i) { if(i > inputs.length) throw new Error("You do not have enough test cases"); var expected = outputs[i]; diff --git a/problems/problem12.js b/problems/problem12.js index 2947d1b..60dccae 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]], + [[0,3], [1,3,4,5]], + [[0,1,2,3], [4,5]], + [[0,1,2,5], [1,5]], + [[0,3,6], [1,3,4,5,6]] ] let outputs = [ - + [0,2,4,5], + [0,1,4,5], + [], + [0,2], + [0,1,4,5] ] /* @@ -20,8 +28,28 @@ uniqueElements([1,2,3], [1,2,3]); // [] uniqueElements(2,3); // undefined, not arrays */ function f(arr1, arr2) { - -} + var arr3 = []; + for(var i = 0; i < arr1.length; i++){ + var match = false; + console.log(arr1[i]) + for(var j = 0; j < arr2.length; j++) { + if (arr1[i] === arr2[j]) { + match = true; + } + } + if (!match) { + arr3.push(arr1[i]) + } + } + return arr3; + } + + function ff(arr1, arr2) { + var out1 = f(arr1, arr2); + var out2 = f(arr2, arr1); + return out1.concat(out2) + } + function runTest(i) { if(i > inputs.length) throw new Error("You do not have enough test cases"); diff --git a/problems/problem13.js b/problems/problem13.js index 90669e3..fa85b5e 100644 --- a/problems/problem13.js +++ b/problems/problem13.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "RADAR", + "kayak", + "javascript", + "welcome", + "hello" ] let outputs = [ - + true, + true, + false, + false, + false ] /* @@ -16,8 +24,10 @@ RADAR -> Yes JAVASCRIPT -> No */ function f(str) { - -} + var cleanStr = str.replace(/[^a-z]/ig, "").toLowerCase(); + var invertedStr = cleanStr.split("").reverse().join(""); + return cleanStr === invertedStr; +} function runTest(i) { if(i > inputs.length) throw new Error("You do not have enough test cases"); diff --git a/problems/problem2.js b/problems/problem2.js index d54ae74..2d5147b 100644 --- a/problems/problem2.js +++ b/problems/problem2.js @@ -2,16 +2,26 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - -] - -let outputs = [ + "", + "abc", + "cba", + "123", + "1a2" + ]; + -] + let outputs = [ + undefined, + "c", + "a", + "3", + "2" + ] // 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; + return str[str.length-1]; } function runTest(i) { @@ -24,4 +34,4 @@ runTest(0); runTest(1); runTest(2); runTest(3); -runTest(4); +runTest(4); \ No newline at end of file diff --git a/problems/problem3.js b/problems/problem3.js index 11358c6..2fe605a 100644 --- a/problems/problem3.js +++ b/problems/problem3.js @@ -3,19 +3,30 @@ var assert = require('assert'); // we need 7 test cases. I've provided 2. let inputs = [ [2, 4], - [-3, 3] + [-3, 3], + [-10,2], + [-3, 9], + [-5, 6], + [4,13], + [1, 3] ] let outputs = [ 6, - 0 + 0, + -8, + 6, + 1, + 17, + 4 ] /* 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(arr) { + if(arr[0] == undefined || arr[1] == undefined || arr[0] == NaN || arr[1] == NaN) return undefined; + return arr[0]+arr[1]; } function runTest(i) { diff --git a/problems/problem4.js b/problems/problem4.js index d30a00c..244229b 100644 --- a/problems/problem4.js +++ b/problems/problem4.js @@ -3,12 +3,24 @@ var assert = require('assert'); // we need 8 test cases. I've provided the first 2 let inputs = [ ["hello", 4], - ["", 2] + ["", 2], + ["world", 3], + ["hello", 5], + ["hello", 1], + ["javascript", 7], + ["javascript", 11], + ["javascript", 0] ] let outputs = [ "o", - undefined + undefined, + "l", + undefined, + "e", + "i", + undefined, + "j" ] /* @@ -20,8 +32,9 @@ f(["", 4]); // undefined f(["abc", 0]); // a */ -function f(arr) { - +function f(str, index) { + return str[index] + if (str[index] === undefined) return undefined } function runTest(i) { diff --git a/problems/problem5.js b/problems/problem5.js index b1e2e44..e709fb4 100644 --- a/problems/problem5.js +++ b/problems/problem5.js @@ -2,18 +2,27 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - [2, 7] + [2, 7], + [2, 18], + [5, 7], + [-2, 22], + [2,6], ] let outputs = [ - 14 + 14, + 36, + 35, + -44, + 12 ] /* 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) { + if(arr[0] == undefined || arr[1] == undefined || arr[0] == NaN || arr[1] == NaN) return undefined; + return arr[0]*arr[1]; } function runTest(i) { diff --git a/problems/problem6.js b/problems/problem6.js index f74431a..3fd3398 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] + ["mult", 20, 10], + ["add", 10, 200], + ["sub", 20, 10], + ["sub", 10, 20], + ["spoof", 20, 10], ] let outputs = [ - 30 + 30, + 200, + 210, + 10, + -10, + undefined ] /* @@ -22,9 +31,16 @@ f(["spoof", 10, 10]); // undefined */ function f(arr) { - + if (arr[0] === "add") { + return arr[1]+arr[2] + } else if (arr[0] === "mult") { + return arr[1]*arr[2] + } else if (arr[0] === "sub") { + return arr[1]-arr[2] + } else return undefined } + function runTest(i) { if(i > inputs.length) throw new Error("You do not have enough test cases"); var expected = outputs[i]; @@ -38,3 +54,7 @@ runTest(2); runTest(3); runTest(4); runTest(5); + + + + diff --git a/problems/problem7.js b/problems/problem7.js index 8eb18e3..bea9c51 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], + ["bo", 6], + ["", 3], + ["foo", -1], + ["hooo", 2], + ["foo", -3], + ["b", 5] ] let outputs = [ - + "foofoofoo", + "bobobobobobo", + undefined, + undefined, + "hooohooo", + undefined, + "bbbbb" ] /* @@ -19,14 +31,22 @@ f(["foo", 3]) // "foofoofoo" f(["fo", 3]) // "fofofo" f(["foo", -1]) // undefined */ -function f(arr) { - + +function f(str,n) { + if(n <= 0 || str === "") return undefined; + var newStr = ""; + for (var i=0; i < n; i++) { + newStr += str + } + return newStr; + } + 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]); + var actual = f(inputs[i][0],inputs[i][1]); assert.deepEqual(actual, expected); } diff --git a/problems/problem8.js b/problems/problem8.js index 6165932..e5fc2c3 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", + "world", + "how are you", + "hello there", + "kayak" ] let outputs = [ - + "olleh", + "dlrow", + "uoy era woh", + "ereht olleh", + "kayak" ] /* @@ -14,9 +22,17 @@ 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 newStr = ""; + for (var i=str.length-1; i >=0; i--) { + newStr += str[i] + } + return newStr; } +//function f(str) { +// return str.split("").reverse().join("") +//} + function runTest(i) { if(i > inputs.length) throw new Error("You do not have enough test cases"); var expected = outputs[i]; diff --git a/problems/problem9.js b/problems/problem9.js index 5c52ef5..e5a9cdb 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -2,19 +2,37 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "you are nice", + "welcome to my country", + "hello there", + "how are you going", + "" ] let outputs = [ - + "nice", + "country", + "there", + "going", + "" ] /* 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 f(string) { + if (string === "") return ""; + var str = string.split(" "); + var longest = 0; + var word = ""; + for (var i = 0; i < str.length; i++) { + if (longest <= str[i].length) { + longest = str[i].length; + word = str[i]; + } + } + return word; } function runTest(i) {