diff --git a/problems/problem1.js b/problems/problem1.js index 6d7505c..9917591 100644 --- a/problems/problem1.js +++ b/problems/problem1.js @@ -2,16 +2,17 @@ var assert = require('assert'); // we need 5 test cases. I provided 1 input let inputs = [ - "" + "aa", '_as', '1111', 'blue', "" ] let outputs = [ - + 'a', '_', '1', 'b', 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 +// 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) { diff --git a/problems/problem10.js b/problems/problem10.js index e7eddde..4de59d6 100644 --- a/problems/problem10.js +++ b/problems/problem10.js @@ -2,11 +2,11 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "we are one", "the three musquaqueers", 'true gods of what?', 'power user', 'POP POP LOL' ] let outputs = [ - + 'We Are One', 'The Three Musquaqueers', 'True Gods Of What?', 'Power User', 'Pop Pop Lol' ] /* @@ -17,7 +17,16 @@ f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong */ function f(str) { - + var lower = str.toLowerCase(); + var splitStr = lower.split(" "); + var toUpper = []; + for (var i= 0; i < splitStr.length; i++) { + var firstLetter = splitStr[i][0]; + firstLetter = firstLetter.toUpperCase(); // ALWAYS ASSIGN THE NEW VALUE TO THE VARIABLE!!! + splitStr[i] = firstLetter + splitStr[i].substring(1); + toUpper.push(splitStr[i]); + } + return toUpper.join(" "); } function runTest(i) { diff --git a/problems/problem11.js b/problems/problem11.js index a9db8bc..bac3e63 100644 --- a/problems/problem11.js +++ b/problems/problem11.js @@ -2,18 +2,31 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + [2, 3, 4], + [10, 5, 15, 20], + ['a', 10,'c', 3, 'b'], + [undefined, 11, 12 ], + [] + ] let outputs = [ - + 9, 50, 13, 23, 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 sum = 0; + for(var i=0; i < arr.length; i++) { + if (typeof arr[i] === 'number') { + sum += arr[i]; + } else if (isNaN(i)){ + i+=1; + } + } + return sum; } function runTest(i) { diff --git a/problems/problem12.js b/problems/problem12.js index 2947d1b..64216b0 100644 --- a/problems/problem12.js +++ b/problems/problem12.js @@ -2,10 +2,15 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + [[0,1,2,3], [1,3,4,5]], + [[],[]], + [[1],[1]], + [['blue']], + [[4,4,5,"a"], [1,4,5]] ] let outputs = [ + [0,2,4,5], [], [], undefined, ["a", 1] ] @@ -19,9 +24,44 @@ 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 f(arr) { +// const arr1 = arr[0]; +// const arr2 = arr[1]; +// let arr3 = []; +// for(let i = 0;i < arr1.length; i++) { +// let +// } +// } +[[0,1,2,3], [1,3,4,5]] + +function f(arr) { + var arr1 = arr[0]; + var arr2 = arr[1]; + var ret = []; + for (var i = 0; i inputs.length) throw new Error("You do not have enough test cases"); @@ -36,3 +76,41 @@ runTest(2); runTest(3); runTest(4); + + +// function memeber (x,lst) { +// for (var i =0; i inputs.length) throw new Error("You do not have enough test cases"); var expected = outputs[i]; diff --git a/problems/problem6.js b/problems/problem6.js index d31ae17..c1686d4 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", 5, 10], + ['sub', 10, 5], //write a different number + ['blue', 6, 8], + [undefined, 9, 0] ] let outputs = [ - 30 + 30, + undefined, + 50, + 5, + undefined, + undefined ] /* @@ -21,8 +30,17 @@ f("mult", 2, 3); // 6 f("spoof", 10, 10); // undefined */ -function f(operation, firstArgument, secondArgument) { - +function f(arr) { + var x; + if (arr[0] === 'add') { + return x = arr[1]+arr[2]; + } else if (arr[0] === 'mult') { + return x = arr[1]*arr[2]; + } else if (arr[0] === 'sub') { + return x = arr[1]-arr[2]; + } else { + return undefined + } } function runTest(i) { diff --git a/problems/problem7.js b/problems/problem7.js index c3bf4b1..ae27734 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -2,11 +2,23 @@ var assert = require('assert'); // we need 7 test cases. let inputs = [ - + ['blue', 2], + ['na na na ', 3], + ['sa', 8], + ['asdasd', 3], + ['true', 'bark'], + [undefined, 4], + ['armor', 0] ] let outputs = [ - + 'blueblue', + 'na na na na na na na na na ', + 'sasasasasasasasa', + 'asdasdasdasdasdasd', + undefined, + undefined, + '' ] /* @@ -19,8 +31,21 @@ f("foo", 3) // "foofoofoo" f("fo", 3) // "fofofo" f("foo", -1) // undefined */ -function f(str, n) { - +function f(str) { + var word = str[0]; + var num = str[1]; + var empty = ''; + if (typeof word !== 'string') { + return undefined; + } else if (isNaN(num)) { + return undefined; + } else if (num <= 0) { + return empty; + } else if (num > 0) { + return word.repeat(num); + } else { + return undefined; + } } function runTest(i) { diff --git a/problems/problem8.js b/problems/problem8.js index 6165932..1b57996 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', + 'blue', + 'red', + 'power', + 'tenacity' ] let outputs = [ - + 'olleh', + 'eulb', + 'der', + 'rewop', + 'yticanet' ] /* @@ -14,7 +22,11 @@ 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 rev = ''; + for (var i = str.length -1;i >= 0;i--) { + rev += str[i] + } + return rev; } function runTest(i) { diff --git a/problems/problem9.js b/problems/problem9.js index 5c52ef5..9dbab5e 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + 'Welcome to coding', + 'blue is true', + 'hello worlds', + 'beautiful line alignment', + '' ] let outputs = [ - + 'Welcome', + 'true', + 'worlds', + 'alignment', + '' ] /* @@ -14,7 +22,15 @@ 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) { - + let words = str.split(" "); + let ret = ""; + for(var i =0; i< words.length;i++){ + let longest = words[i]; + if (longest.length >= ret.length) { + ret = longest; + } + } + return ret; } function runTest(i) { diff --git a/problems/test.js b/problems/test.js new file mode 100644 index 0000000..43a1305 --- /dev/null +++ b/problems/test.js @@ -0,0 +1,13 @@ +function m(x) { + return x * 2; +} + +function k(f, i, j) { + return f(i + 2) + j * 2; +} + +function f(g, h) { + return g(h, 1, 4); +} + +console.log(f(k, m)); \ No newline at end of file