diff --git a/problems/.vscode/launch.json b/problems/.vscode/launch.json new file mode 100644 index 0000000..27d3060 --- /dev/null +++ b/problems/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${file}" + } + ] +} \ No newline at end of file diff --git a/problems/problem1.js b/problems/problem1.js index 6d7505c..95ef6f6 100644 --- a/problems/problem1.js +++ b/problems/problem1.js @@ -1,27 +1,39 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. I provided 1 input let inputs = [ - "" + "", + "abc", + "cba", + "123", + "321" ] let outputs = [ - + undefined, + "a", + "c", + "1", + "3" ] // 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) { - + var array = str.split(""); + var firstLetter = array[0]; + return firstLetter; } 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); runTest(1); runTest(2); runTest(3); -runTest(4); +runTest(4); \ No newline at end of file diff --git a/problems/problem10.js b/problems/problem10.js index e7eddde..d5b927c 100644 --- a/problems/problem10.js +++ b/problems/problem10.js @@ -1,12 +1,22 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "Hello world", + "Bonjour le monde", + "ola sénorita", + "hELLO eVERYONE iN tHE wORLD", + "Koalas are cool" ] let outputs = [ - + "Hello World", + "Bonjour Le Monde", + "Ola Sénorita", + "Hello Everyone In The World", + "Koalas Are Cool" ] /* @@ -17,11 +27,23 @@ f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong */ function f(str) { - + var arr = str.toLowerCase().split(" "); + var firstChar = ""; + var capWord = ""; + var newArr = []; + + for (var i = 0; i < arr.length; ++i) { + firstChar = arr[i].toUpperCase().charAt(0); + capWord = firstChar + arr[i].substring(1, arr[i].length); + newArr.push(capWord); + } + + var newStr = newArr.join(" "); + return newStr; } function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); + 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); diff --git a/problems/problem11.js b/problems/problem11.js index a9db8bc..2ff5c84 100644 --- a/problems/problem11.js +++ b/problems/problem11.js @@ -1,23 +1,43 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - + [10, 3, 5, 3, 2], + [4, 3, 1, 6, 4, 2, 4], + [5, 3, 7, 9, 5, 6], + ["hi", 4, 6, 3, 2, "", 5], + [] ] let outputs = [ - + 23, + 24, + 35, + 20, + 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 (0 === arr.length) return 0; + + for (var i = 0; i < arr.length; ++i) { + if ((isNaN(arr[i]) || arr[i] === ("" || ''))) { + arr.splice(i, 1, 0); + } + } + + return arr.reduce(function (a, b) { + return a + b; + }); } function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); + 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); diff --git a/problems/problem12.js b/problems/problem12.js index 2947d1b..513da3c 100644 --- a/problems/problem12.js +++ b/problems/problem12.js @@ -1,12 +1,22 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - + [[1, 2, 3], [4, 5, 6]], + [[1, 2, 3, 4, 5, 6], [4, 5, 6]], + [[1, 2, 3], [1, 2, 3, 4, 5, 6]], + [[1, 3, 4], [1, 2, 3]], + [[1, 2, 3, 5], [2, 4, 5, 6]] ] let outputs = [ - + [1, 2, 3, 4, 5, 6], + [1, 2, 3], + [4, 5, 6], + [4, 2], + [1, 3, 4, 6] ] /* @@ -19,12 +29,13 @@ 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) { + var newArr = !Array.isArray(arr[0]) && !Array.isArray(arr[1]) ? undefined : arr[0].concat(arr[1]).filter(e => arr[0].includes(e) && !arr[1].includes(e) || !arr[0].includes(e) && arr[1].includes(e)); + return newArr; } function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); + 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); diff --git a/problems/problem13.js b/problems/problem13.js index 90669e3..743b57b 100644 --- a/problems/problem13.js +++ b/problems/problem13.js @@ -1,12 +1,22 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "racecar", + "butter", + "computer", + "kayak", + "bottom" ] let outputs = [ - + true, + false, + false, + true, + false ] /* @@ -16,11 +26,22 @@ RADAR -> Yes JAVASCRIPT -> No */ function f(str) { - + var a = str; + var newStr = str.split(''); + var newWord = []; + for (var i = 0; i <= str.length; ++i) { + newWord.push(newStr[str.length - i]); + } + + var b = newWord.join(''); + + if (a == b) return true; + + else return false; } function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); + 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); diff --git a/problems/problem14.js b/problems/problem14.js index eb49b73..9016c91 100644 --- a/problems/problem14.js +++ b/problems/problem14.js @@ -1,12 +1,22 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "01234567890123456789012345678901234567890123456789012345678901234567890123456789", + "0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789", + "0123456789012345678901234567890123456789 0123456789012345678901234567890123456789", + " Second row", + "0123456789012345678901234567890123456789 9" ] let outputs = [ - + "0123456789012345678901234567890123456789\n0123456789012345678901234567890123456789", + "0123456789 0123456789 0123456789 0123456\n789 0123456789 0123456789 0123456789", + "0123456789012345678901234567890123456789\n0123456789012345678901234567890123456789", + "Second row", + "0123456789012345678901234567890123456789\n9" ] /* @@ -30,12 +40,34 @@ Lorem ipsumos dolor sit amet consectetur even though there is a space before the a in adipisicing */ + +// 1- Verifies is loop is necessary +// 2- Loop to change line at every 40 characters +// 3- Verifies if there is more text modify +// 4- Verifies if the first character of a new line would be an empty character +// 5- If that happens, it simply skips it and verifies the next character +// 6- No risk of this never ending or going to the end of the string because of str.trim() at beginning +// 7- Extracts 40 characters strating from the first index and then places the extracted string into the array's next position + function f(str) { - + var newStr = str.trim(); + if (newStr.length < 40) return newStr; // *1 + var i = 0; + var j = 0; + var arr = []; + var endLoop = true; + while (endLoop === true) { // *2 + if (newStr.length > (40 * i) + j) { // *3 + if (newStr.charAt((40 * i) + j) === " ") while (newStr.charAt((40 * i) + j) === " ")++j; // *4*5*6 + arr[i] = newStr.slice((40 * i) + j, (40 * (i + 1)) + j); // *7 + } + else endLoop = false; + ++i; + } return arr.join("\n"); } function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); + 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); @@ -45,5 +77,4 @@ runTest(0); runTest(1); runTest(2); runTest(3); -runTest(4); - +runTest(4); \ No newline at end of file diff --git a/problems/problem15.js b/problems/problem15.js index eb49b73..9016c91 100644 --- a/problems/problem15.js +++ b/problems/problem15.js @@ -1,12 +1,22 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "01234567890123456789012345678901234567890123456789012345678901234567890123456789", + "0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789", + "0123456789012345678901234567890123456789 0123456789012345678901234567890123456789", + " Second row", + "0123456789012345678901234567890123456789 9" ] let outputs = [ - + "0123456789012345678901234567890123456789\n0123456789012345678901234567890123456789", + "0123456789 0123456789 0123456789 0123456\n789 0123456789 0123456789 0123456789", + "0123456789012345678901234567890123456789\n0123456789012345678901234567890123456789", + "Second row", + "0123456789012345678901234567890123456789\n9" ] /* @@ -30,12 +40,34 @@ Lorem ipsumos dolor sit amet consectetur even though there is a space before the a in adipisicing */ + +// 1- Verifies is loop is necessary +// 2- Loop to change line at every 40 characters +// 3- Verifies if there is more text modify +// 4- Verifies if the first character of a new line would be an empty character +// 5- If that happens, it simply skips it and verifies the next character +// 6- No risk of this never ending or going to the end of the string because of str.trim() at beginning +// 7- Extracts 40 characters strating from the first index and then places the extracted string into the array's next position + function f(str) { - + var newStr = str.trim(); + if (newStr.length < 40) return newStr; // *1 + var i = 0; + var j = 0; + var arr = []; + var endLoop = true; + while (endLoop === true) { // *2 + if (newStr.length > (40 * i) + j) { // *3 + if (newStr.charAt((40 * i) + j) === " ") while (newStr.charAt((40 * i) + j) === " ")++j; // *4*5*6 + arr[i] = newStr.slice((40 * i) + j, (40 * (i + 1)) + j); // *7 + } + else endLoop = false; + ++i; + } return arr.join("\n"); } function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); + 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); @@ -45,5 +77,4 @@ runTest(0); runTest(1); runTest(2); runTest(3); -runTest(4); - +runTest(4); \ No newline at end of file diff --git a/problems/problem16.js b/problems/problem16.js index 9880f09..0a3d910 100644 --- a/problems/problem16.js +++ b/problems/problem16.js @@ -1,3 +1,4 @@ +// SOLVED! var assert = require('assert'); @@ -10,7 +11,7 @@ let inputs = [ ] let outputs = [ - 1, + 01, 5, 9, 18, @@ -18,7 +19,7 @@ let outputs = [ ] function f(digit) { - return digit%10 + (digit - digit % 10) / 10; + return digit % 10 + (digit - digit % 10) / 10; } function runTest(i) { diff --git a/problems/problem2.js b/problems/problem2.js index d54ae74..537831b 100644 --- a/problems/problem2.js +++ b/problems/problem2.js @@ -1,17 +1,30 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "", + "abc", + "cba", + "123", + "321" ] let outputs = [ - + undefined, + "c", + "a", + "3", + "1" ] // 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) { - + var array = str.split(""); + var reversed = array.reverse(); + var firstLetter = reversed[0]; + return firstLetter; } function runTest(i) { diff --git a/problems/problem3.js b/problems/problem3.js index 11358c6..5db2f60 100644 --- a/problems/problem3.js +++ b/problems/problem3.js @@ -1,27 +1,43 @@ +// SOLVED! + var assert = require('assert'); // we need 7 test cases. I've provided 2. let inputs = [ [2, 4], - [-3, 3] + [-3, 3], + ["", 3], + ["ididn", 3], + [5, 54], + [5, 2], + [10, 4] ] let outputs = [ 6, - 0 + 0, + undefined, + undefined, + 59, + 7, + 14 ] /* 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) { + if ((!isNaN(x[0]) && !isNaN(x[1])) && (x[0] !== ("" || '') && x[1] !== ("" || ''))) { + var result = x[0] + x[1]; + return result; + } + return undefined; } 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..a9b8e70 100644 --- a/problems/problem4.js +++ b/problems/problem4.js @@ -1,34 +1,49 @@ +// SOLVED! + var assert = require('assert'); // we need 8 test cases. I've provided the first 2 let inputs = [ ["hello", 4], - ["", 2] + ["", 2], + ["abc", 1], + ["chess", 3], + ["car", 2], + ["doggy", 3], + ["felt", 3], + ["chocolate", 5], ] let outputs = [ "o", - undefined + undefined, + "b", + "s", + "r", + "g", + "t", + "l" ] /* 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) { - +function f(str, num) { + if (str.length === 0) return undefined; + var index = str.charAt(num); + if (index === 0) return undefined; + return index; } function runTest(i) { - var expected = outputs[i]; - var input = inputs[i]; - var actual = f(input[0], input[1]); - assert.deepEqual(actual, expected); + var expected = outputs[i]; + var input = inputs[i]; + var actual = f(input[0], input[1]); + assert.deepEqual(actual, expected); } runTest(0); @@ -38,4 +53,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..b5307e4 100644 --- a/problems/problem5.js +++ b/problems/problem5.js @@ -1,26 +1,40 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - [2, 7] + [2, 7], + [3, 3], + ["ifjew", 5], + [5, 10], + [4, ''] ] let outputs = [ - 14 + 14, + 9, + undefined, + 50, + 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])) && (x[0] !== ("" || '') && x[1] !== ("" || ''))) { + var result = x[0] * x[1]; + return result; + } + return undefined; } 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..ee09b59 100644 --- a/problems/problem6.js +++ b/problems/problem6.js @@ -1,14 +1,25 @@ +// SOLVED! + // pro tip: use nodemon instead of node var assert = require('assert'); // we need 6 test cases. let inputs = [ ["add", 10, 20], - ["chair", 20, 10] + ["chair", 20, 10], + ["mult", 5, 10], + ["sub", 4, 6], + ["add", 30, 4], + ["mult", 3, 3] ] let outputs = [ - 30 + 30, + undefined, + 50, + -2, + 34, + 9 ] /* @@ -16,20 +27,36 @@ 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 result; + + switch (arr[0]) { + case "add": + result = arr[1] + arr[2]; + return result; + + case "sub": + result = arr[1] - arr[2]; + return result; + + case "mult": + result = arr[1] * arr[2]; + return result; + + default: + return undefined; + } } 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); @@ -37,4 +64,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..31ee8e1 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -1,30 +1,53 @@ +// SOLVED! + var assert = require('assert'); // we need 7 test cases. let inputs = [ - + [24, 4], + ["hubba", 3], + ["boots and cats ", 10], + ["racecar", 3], + ["lol", ""], + ["yolo", 0], + ["BLARGH!", -2] ] let outputs = [ - + undefined, + "hubbahubbahubba", + "boots and cats boots and cats boots and cats boots and cats boots and cats boots and cats boots and cats boots and cats boots and cats boots and cats ", + "racecarracecarracecar", + 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 = ""; + + if ((!isNaN(arr[0])) || (arr[1] === ("" || ''))) return undefined; + + else if ((arr[1] <= 0) && !isNaN(arr[1])) return str; + + else if ((isNaN(arr[0]) && arr[0] !== ("" || '')) && !isNaN(arr[1])) { + str = arr[0].repeat(arr[1]); + return str; + } + + return undefined; } function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); + 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); @@ -37,4 +60,3 @@ runTest(3); runTest(4); runTest(5); runTest(6); - diff --git a/problems/problem8.js b/problems/problem8.js index 6165932..0e095f0 100644 --- a/problems/problem8.js +++ b/problems/problem8.js @@ -1,12 +1,22 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "racecar", + "butter", + "computer", + "jar", + "bottom" ] let outputs = [ - + "racecar", + "rettub", + "retupmoc", + "raj", + "mottob" ] /* @@ -14,11 +24,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 = str.split(''); + var newWord = []; + for (var i = 0; i <= str.length; ++i) { + newWord.push(newStr[str.length - i]); + } + + return newWord.join(''); } function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); + 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); diff --git a/problems/problem9.js b/problems/problem9.js index 5c52ef5..cfc875a 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -1,12 +1,22 @@ +// SOLVED! + var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "hi", + "", + "Hello my anticonstitutionnalisationnable people", + "One two three", + "Racecar up side down is racecar" ] let outputs = [ - + "hi", + "", + "anticonstitutionnalisationnable", + "three", + "racecar" ] /* @@ -14,11 +24,16 @@ 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) { - + str.split(" ").toLowerCase(); + var lgStr = ""; + for (var i = 0; i < str.length; ++i) { + if (lgStr.length <= str[i].length) lgStr = str[i]; + } + return lgStr; } function runTest(i) { - if(i > inputs.length) throw new Error("You do not have enough test cases"); + 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);