diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..48e341a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +} diff --git a/problem1.js b/problem1.js new file mode 100644 index 0000000..285f5b9 --- /dev/null +++ b/problem1.js @@ -0,0 +1,37 @@ +var assert = require('assert'); + +// we need 5 test cases. I provided 1 input +let inputs = [ + "", + "ab", + "abc", + "de", + "fa" +] + +let outputs = [ + undefined, + "a", + "a", + "d", + "f" +] + +// 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) { + + return str[0]; + +} + +function runTest(i) { + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); diff --git a/problem10.js b/problem10.js new file mode 100644 index 0000000..27a15af --- /dev/null +++ b/problem10.js @@ -0,0 +1,52 @@ +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "helo world", + "ALL YOUR BASE ARE BELONG", + "MaMa sita", + "Noé provost", + "b a d de" + +] + +let outputs = [ + "Helo World", + "All Your Base Are Belong", + "Mama Sita", + "Noé Provost", + "B A D De" +] + +/* +Make this function return the input string, capitalized. You must use a for loop. For example: + +f("hello world"); // Hello World +f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong + +*/ + + +function f(str) { + var words = str.split(" ") + var ret = ""; + for(var i = 0; i < words.length; i++) { + ret = ret + words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase()+" " + + } + return ret; + } + +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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); + diff --git a/problem11.js b/problem11.js new file mode 100644 index 0000000..bc5604a --- /dev/null +++ b/problem11.js @@ -0,0 +1,50 @@ +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + [1,2,3], + [2,2], + [1,1,1], + [1,"a",1,2], + [1] +] + +let outputs = [ + 6, + 4, + 3, + 4, + 1 + +] + +/* +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 x = 0; + for (i=0; i<= arr.length-1; i++) { + + if (isNaN(arr[i]) === true) { + x= x+0; + } + else { + x = x + arr[i]; + } + } + return x; +} + +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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); + diff --git a/problem12.js b/problem12.js new file mode 100644 index 0000000..6d35761 --- /dev/null +++ b/problem12.js @@ -0,0 +1,77 @@ +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + [[],"allo"], + [[1],[1]], + [[1],[2]], + [[1,2,3],[2,3,5]], + [[1,4,2],[2,3,2]], + + ] + + let outputs = [ + undefined, + [], + [1,2], + [1,5], + [1,4,3] + + ] + +/* +Make this function return the elements that are unique to array1 and array2. +If there are no unique elements return an empty array. +If the inputs are anything other than arrays, return undefined. +For example: + +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) { + var arr3 = []; + var count = 0; + if( Array.isArray(arr1) === false || Array.isArray(arr2) === false ){ + return undefined; + } + for (i=0; i<= arr1.length-1 ;i++){ + count = 0; + for (y=0; y<= arr2.length-1; y++){ + if(arr1[i]===arr2[y]){ + count = count + 1; + } + else if(count === 0 && y === arr2.length-1){ + arr3= arr3 + arr1[i]; + } + + } + } + for (a=0; a<= arr2.length-1 ;a++){ + count = 0; + for (b=0; b<= arr1.length-1; b++){ + if(arr2[a] ===arr1[b]){ + count = count + 1; + } + else if(count === 0 && b === arr1.length-1){ + arr3= arr3 + arr2[a]; + } + + } + } + return arr3; + } + +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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); + diff --git a/problem13.js b/problem13.js new file mode 100644 index 0000000..0919df5 --- /dev/null +++ b/problem13.js @@ -0,0 +1,59 @@ +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "radar", + "alla", + "non", + "fafrea", + "abc" +] + +let outputs = [ + true, + true, + true, + false, + false +] + +/* +Make this function return true if the input string is a palindrome, and false otherwise. A palindrome is simply a string that is the same if you reverse it. + +RADAR -> Yes +JAVASCRIPT -> No +*/ +function f(str) { + + var characters = str.split(''); + + var firstLetter = characters.shift(); + + var lastLetter = characters.pop(); + + if (firstLetter !== lastLetter) { + return false; + } + + if (characters.length < 2) { + return true; + } + + return f(characters.join('')); + +} + + +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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); + diff --git a/problem14.js b/problem14.js new file mode 100644 index 0000000..eb49b73 --- /dev/null +++ b/problem14.js @@ -0,0 +1,49 @@ +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + +] + +let outputs = [ + +] + +/* +Make this function return the input string wrapped to 40 characters per line. +This means you'll have to insert a newline \n character after every 40 characters in the input string. +If the next character after a cut is a space, then do not display it. + +For example with the input: + +Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam + +the output would be: + +Lorem ipsumos dolor sit amet consectetur +adipisicing elit. Magni quisquam + +instead of: + +Lorem ipsumos dolor sit amet consectetur + adipisicing elit. Magni quisquam + + even though there is a space before the a in adipisicing +*/ +function f(str) { + +} + +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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); + diff --git a/problem15.js b/problem15.js new file mode 100644 index 0000000..eb49b73 --- /dev/null +++ b/problem15.js @@ -0,0 +1,49 @@ +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + +] + +let outputs = [ + +] + +/* +Make this function return the input string wrapped to 40 characters per line. +This means you'll have to insert a newline \n character after every 40 characters in the input string. +If the next character after a cut is a space, then do not display it. + +For example with the input: + +Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam + +the output would be: + +Lorem ipsumos dolor sit amet consectetur +adipisicing elit. Magni quisquam + +instead of: + +Lorem ipsumos dolor sit amet consectetur + adipisicing elit. Magni quisquam + + even though there is a space before the a in adipisicing +*/ +function f(str) { + +} + +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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); + diff --git a/problem16.js b/problem16.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/problem16.js @@ -0,0 +1 @@ + diff --git a/problem2.js b/problem2.js new file mode 100644 index 0000000..b20a765 --- /dev/null +++ b/problem2.js @@ -0,0 +1,35 @@ +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ +"abc", +"def", +"deg", +"der", +"da", +] + +let outputs = [ + "c", + "f", + "g", + "r", + "a" +] + +// 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) { + return str[str.length-1] +} + +function runTest(i) { + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); diff --git a/problem3.js b/problem3.js new file mode 100644 index 0000000..d0e41c4 --- /dev/null +++ b/problem3.js @@ -0,0 +1,50 @@ +var assert = require('assert'); + +// we need 7 test cases. I've provided 2. +let inputs = [ + [2, 4], + [-3, 3], + ["a", 4], + [1, 2], + [10, -10], + ["a", 3], + [1, 2] + +] + +let outputs = [ + 6, + 0, + undefined, + 3, + 0, + undefined, + 3 +] + +/* +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) { + if (isNaN(parseInt(x[0])) || isNaN(parseInt(x[1]))) { + return undefined; + } + + else { + return (x[1] + x[0]); + } +} + +function runTest(i) { + var expected = outputs[i]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); +runTest(5); +runTest(6); diff --git a/problem4.js b/problem4.js new file mode 100644 index 0000000..8370cbd --- /dev/null +++ b/problem4.js @@ -0,0 +1,62 @@ +var assert = require('assert'); + +// we need 8 test cases. I've provided the first 2 +let inputs = [ + ["hello", 4], + ["", 2], + ["hello", 1], + ["hello", 0], + ["abc", 0] + ["4", 4], + ["a",0], + ["1234", 3] + +] + +let outputs = [ + "o", + undefined, + "e", + "h", + "a", + undefined, + "a", + "4" +] + +/* +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 + +*/ +function f(str, num) { + + var res = str.charAt(num); + if(res === ""){ + return undefined; + } + else { + return res; + } + +} + +function runTest(i) { + var expected = outputs[i]; + var input = inputs[i]; + var actual = f(input[0], input[1]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); +runTest(5); +runTest(6); +runTest(7); diff --git a/problem5.js b/problem5.js new file mode 100644 index 0000000..3ecfde0 --- /dev/null +++ b/problem5.js @@ -0,0 +1,47 @@ +import { fail } from 'assert'; + +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + [2, 7], + [4, 5], + ["d", 7], + ["a", 7], + ["", 3] +] + +let outputs = [ + 14, + 20, + undefined, + 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) { + if(isNaN(parseInt(x)) === true ||isNaN(parseInt(y)) === true ) { + return undefined; + } + else { + return (x*y); + } + +} + + +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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); diff --git a/problem6.js b/problem6.js new file mode 100644 index 0000000..cdbae01 --- /dev/null +++ b/problem6.js @@ -0,0 +1,63 @@ +// pro tip: use nodemon instead of node +var assert = require('assert'); + +// we need 6 test cases. +let inputs = [ + ["add", 10, 20], + ["chair", 20, 10], + ["mult", 2, 3], + ["spoof", 10, 10], + ["sub", 10, 3], + ["spodof", 100, 100], + +] + +let outputs = [ + 30, + undefined, + 6, + undefined, + 7, + undefined + +] + +/* +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 + +*/ +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]; + var actual = f(inputs[i]); + assert.deepEqual(actual, expected); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); +runTest(5); diff --git a/problem7.js b/problem7.js new file mode 100644 index 0000000..0022bad --- /dev/null +++ b/problem7.js @@ -0,0 +1,67 @@ +var assert = require('assert'); + +// we need 7 test cases. +let inputs = [ + ["foo", 3], + ["fo", 3], + ["foo", -1], + [4 , 5], + ["allo", 1], + ["ca", 4], + ["pi",0] + + +] + +let outputs = [ + "foofoofoo", + "fofofo", + "", + undefined, + "allo", + "cacacaca", + "" +] + +/* +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 +*/ +function f(arr) { + function isString(x) { + return Object.prototype.toString.call(x) === "[object String]" + } + + if(isString(arr[0]) === false){ + return undefined; + } + else if (arr[1]<=0) + return ""; + + else { + return (arr[0].repeat(arr[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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); +runTest(5); +runTest(6); + diff --git a/problem8.js b/problem8.js new file mode 100644 index 0000000..cc44d01 --- /dev/null +++ b/problem8.js @@ -0,0 +1,45 @@ +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "hello", + "how are you", + "cac", + "loaf", + "pouf" +] + +let outputs = [ + "olleh", + "uoy era woh", + "cac", + "faol", + "fuop" +] + +/* +Make this function return the input string, reversed. For example "hello" would return "olleh" and "how are you" would return "uoy era woh". +You must use a for loop for this exercise. +*/ +function f(str) { + var newString = ""; + for (var i = str.length - 1; i >= 0; i--) { + newString = newString + str[i]; + + } + return newString; +} + +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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); + diff --git a/problem9.js b/problem9.js new file mode 100644 index 0000000..d22dc48 --- /dev/null +++ b/problem9.js @@ -0,0 +1,49 @@ +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "", + "word", + "ab abc", + "abc cba", + "cd cba ab abc ac" +] + +let outputs = [ + "", + "word", + "abc", + "cba", + "abc" +] + +/* +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) { + var splitStr = str.split(" "); + var ret = ""; + for(var i =0; i < splitStr.length; i++){ + + var theString =splitStr[i]; + if(theString.length >= ret.length){ + ret = theString; + } + } + return ret; +} + +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); +} + +runTest(0); +runTest(1); +runTest(2); +runTest(3); +runTest(4); +