From 94693a57c91abe59f935b223027227b98de3bd6d Mon Sep 17 00:00:00 2001 From: jytsong Date: Thu, 11 Jan 2018 00:33:22 -0500 Subject: [PATCH] done --- .vscode/launch.json | 26 +++++----- problems/problem1.js | 63 +++++++++++++---------- problems/problem10.js | 83 +++++++++++++++++------------- problems/problem11.js | 75 +++++++++++++++------------ problems/problem12.js | 93 ++++++++++++++++++++-------------- problems/problem13.js | 76 +++++++++++++++------------- problems/problem14.js | 115 ++++++++++++++++++++++++------------------ problems/problem15.js | 98 +++++++++++++++++------------------ problems/problem16.js | 32 ------------ problems/problem2.js | 63 +++++++++++++---------- problems/problem3.js | 78 ++++++++++++++++------------ problems/problem4.js | 95 +++++++++++++++++++--------------- problems/problem5.js | 70 ++++++++++++++----------- problems/problem6.js | 94 +++++++++++++++++++--------------- problems/problem7.js | 94 +++++++++++++++++++--------------- problems/problem8.js | 79 +++++++++++++++++------------ problems/problem9.js | 78 ++++++++++++++++------------ 17 files changed, 729 insertions(+), 583 deletions(-) delete mode 100644 problems/problem16.js diff --git a/.vscode/launch.json b/.vscode/launch.json index b1583b7..1f5daf4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,14 +1,14 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "type": "node", - "request": "launch", - "name": "Launch Program", - "program": "${file}", - "skipFiles": [ - "node_modules", "assertion.js" - ] - } - ] +{ + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${file}", + "skipFiles": [ + "node_modules", "assertion.js" + ] + } + ] } \ No newline at end of file diff --git a/problems/problem1.js b/problems/problem1.js index 6d7505c..6a304c5 100644 --- a/problems/problem1.js +++ b/problems/problem1.js @@ -1,27 +1,36 @@ -var assert = require('assert'); - -// we need 5 test cases. I provided 1 input -let inputs = [ - "" -] - -let outputs = [ - -] - -// 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) { - -} - -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); +var assert = require('assert'); + +// we need 5 test cases. I provided 1 input +let inputs = [ + "abc", + "123", + "!ok", + "1a^", + "" +] + +let outputs = [ + "a", + "1", + "!", + "1", + 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 +function f(str) { + var char1 = str[0]; + return char1; +} + +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/problems/problem10.js b/problems/problem10.js index e7eddde..ad7f77d 100644 --- a/problems/problem10.js +++ b/problems/problem10.js @@ -1,35 +1,48 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -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) { - -} - -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); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + 'hello there', + 'alOha', + '', + 'bOnjOur aMIGO', + 'h3ll0' +] + +let outputs = [ + 'Hello There', + 'Aloha', + '', + 'Bonjour Amigo', + 'H3ll0' +] + +/* +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) { + str = str.toLowerCase().split(' '); + for (var i=0; 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/problems/problem11.js b/problems/problem11.js index a9db8bc..4ef1159 100644 --- a/problems/problem11.js +++ b/problems/problem11.js @@ -1,31 +1,44 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -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) { - -} - -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); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + [0, 5, 15], + [2, -3], + [1, 'plus', 1], + ['ok', 6, 15, 'ok'], + [] //what should we do if ALL elements are not numbers? Treat as empty? +] + +let outputs = [ + 20, + -1, + 2, + 21, + 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; + if (arr.length === 0) {return 0;} + for (var i=0; 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/problems/problem12.js b/problems/problem12.js index 2947d1b..abec2bf 100644 --- a/problems/problem12.js +++ b/problems/problem12.js @@ -1,38 +1,55 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -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) { - -} - -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); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + [[1, 2, 3, 4, 5],[1, 2, 5]] +] + +let outputs = [ + [3, 4] +] + +/* +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) { + if (arr1 instanceof Array && arr2 instanceof Array) { + var newArr = []; + var deleteflag = 0; + for (var i=0; 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/problems/problem13.js b/problems/problem13.js index 90669e3..81ae4db 100644 --- a/problems/problem13.js +++ b/problems/problem13.js @@ -1,34 +1,42 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -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) { - -} - -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); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + 'radar', + 'civic', + '123321', + 'hello', + '1racecar1' +] + +let outputs = [ + true, + true, + true, + false, + true +] + +/* +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) { + return if (str == str.reverse()); +} + +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/problems/problem14.js b/problems/problem14.js index eb49b73..d13db03 100644 --- a/problems/problem14.js +++ b/problems/problem14.js @@ -1,49 +1,66 @@ -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); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + 'Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam', + 'Hi there, my name is Jimmy. I am writing a 40 character test.', + 'Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam', + 'Hi there, my name is Jimmy. I am writing a 40 character test.', + 'Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam' +] + +let outputs = [ + 'Lorem ipsumos dolor sit amet consectetur\nadipisicing elit. Magni quisquam', + "Hi there, my name is Jimmy. I am writing\na 40 character test.", + 'Lorem ipsumos dolor sit amet consectetur\nadipisicing elit. Magni quisquam', + 'Lorem ipsumos dolor sit amet consectetur\nadipisicing elit. Magni quisquam', + "Hi there, my name is Jimmy. I am writing\na 40 character test." +] + +/* +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) { + var n = Math.floor(str.length / 40); + var newStr = str.split(''); + for (var i = 1; i <= n; i++) { + if (newStr[i * 40] !== ' ') { + newStr.splice(i * 40, 0, '\n'); + + } + else if (newStr[i * 40] == ' ') { newStr.splice(i * 40, 1, '\n'); } + } + return newStr.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/problems/problem15.js b/problems/problem15.js index eb49b73..cf1cfa1 100644 --- a/problems/problem15.js +++ b/problems/problem15.js @@ -1,49 +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); - +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/problems/problem16.js b/problems/problem16.js deleted file mode 100644 index 9880f09..0000000 --- a/problems/problem16.js +++ /dev/null @@ -1,32 +0,0 @@ - -var assert = require('assert'); - -let inputs = [ - 10, - 23, - 45, - 99, - 19 -] - -let outputs = [ - 1, - 5, - 9, - 18, - 10 -] - -function f(digit) { - return digit%10 + (digit - digit % 10) / 10; -} - -function runTest(i) { - assert.deepEqual(f(inputs[i]), outputs[i]); -} - -runTest(0); -runTest(1); -runTest(2); -runTest(3); -runTest(4); diff --git a/problems/problem2.js b/problems/problem2.js index d54ae74..097f739 100644 --- a/problems/problem2.js +++ b/problems/problem2.js @@ -1,27 +1,36 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -// 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) { - -} - -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); +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ +"123", +"abc", +"", +"0chsf1", +"0b5b!" +] + +let outputs = [ + "3", + "c", + undefined, + "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 lastchar = str[str.length-1]; + return lastchar; +} + +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/problems/problem3.js b/problems/problem3.js index 11358c6..d5d9752 100644 --- a/problems/problem3.js +++ b/problems/problem3.js @@ -1,33 +1,45 @@ -var assert = require('assert'); - -// we need 7 test cases. I've provided 2. -let inputs = [ - [2, 4], - [-3, 3] -] - -let outputs = [ - 6, - 0 -] - -/* -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 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); +var assert = require('assert'); + +// we need 7 test cases. I've provided 2. +let inputs = [ + [2, 4], + [-3, 3], + [1, 't'], + ['t', 't'], + [0, 5], + [15, '!'], + [50, 51] +] + +let outputs = [ + 6, + 0, + undefined, + undefined, + 5, + undefined, + 101 +] + +/* +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(arr) { + sum = arr[0] + arr[1]; + if (typeof sum == 'number') {return sum;} + else {return undefined;} + } + +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/problems/problem4.js b/problems/problem4.js index 4082082..f3215bc 100644 --- a/problems/problem4.js +++ b/problems/problem4.js @@ -1,41 +1,54 @@ -var assert = require('assert'); - -// we need 8 test cases. I've provided the first 2 -let inputs = [ - ["hello", 4], - ["", 2] -] - -let outputs = [ - "o", - undefined -] - -/* -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, index) { - -} - -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); +var assert = require('assert'); + +// we need 8 test cases. I've provided the first 2 +let inputs = [ + ["hello", 4], + ["", 2], + ["hi", 7], + ['sup!', 'sup'], + ["hi", -1], + ['hell!no', 4], + ['514-555-5555', 10], + ['were', '2'] +] + +let outputs = [ + "o", + undefined, + undefined, + undefined, + undefined, + '!', + '5', + undefined +] + +/* +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, index) { + if (str.length < 1 || str.length < index || typeof str != 'string' || typeof index != 'number') {return undefined;} + return str[index]; +} + +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/problems/problem5.js b/problems/problem5.js index b1e2e44..daf36a4 100644 --- a/problems/problem5.js +++ b/problems/problem5.js @@ -1,30 +1,40 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - [2, 7] -] - -let outputs = [ - 14 -] - -/* -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 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); +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + [2, 7], + [3, 3], + [-5, 6], + [2, 0], + [2, 'polkadots'], +] + +let outputs = [ + 14, + 9, + -30, + 0, + 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) { + var product = x[0]*x[1]; + if (isNaN(product)) {return undefined;} + return product; +} + +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/problems/problem6.js b/problems/problem6.js index d31ae17..4fca14b 100644 --- a/problems/problem6.js +++ b/problems/problem6.js @@ -1,40 +1,54 @@ -// pro tip: use nodemon instead of node -var assert = require('assert'); - -// we need 6 test cases. -let inputs = [ - ["add", 10, 20], - ["chair", 20, 10] -] - -let outputs = [ - 30 -] - -/* -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(operation, firstArgument, secondArgument) { - -} - -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); +// pro tip: use nodemon instead of node +var assert = require('assert'); + +// we need 6 test cases. +let inputs = [ + ["add", 10, 20], + ["chair", 20, 10], + ['sub', 5, 1], + ['mult', 8, 8], + ['sleep', 0, 10], + ['mult', '5', 5] +] + +let outputs = [ + 30, + undefined, + 4, + 64, + undefined, + 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) { + var final; + if (typeof arr[1] !== 'number' || typeof arr[2] !== 'number') {return undefined;} + if (arr[0] === 'add') {final = arr[1]+arr[2]; return final;} + else if (arr[0] === 'mult') {final = arr[1]*arr[2]; return final;} + else if (arr[0] === 'sub') {final = arr[1]-arr[2]; return final;} + 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/problems/problem7.js b/problems/problem7.js index c3bf4b1..6eed120 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -1,40 +1,54 @@ -var assert = require('assert'); - -// we need 7 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -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(str, n) { - -} - -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); - +var assert = require('assert'); + +// we need 7 test cases. +let inputs = [ + ['hi', 4], + ['hey', 1], + ['.', 3], + ['', 15], + ['hello', -4], + ['hola', 'aloha'], + ['salut', 0] +] + +let outputs = [ + 'hihihihi', + 'hey', + '...', + '', + '', + 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 +*/ +function f(arr) { + if (arr[1] === 0 || arr[1] < 0) {return '';} + if (typeof arr[0] !== 'string' || typeof arr[1] !== 'number' ) {return undefined;} + 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/problems/problem8.js b/problems/problem8.js index 6165932..b4dcab8 100644 --- a/problems/problem8.js +++ b/problems/problem8.js @@ -1,32 +1,47 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -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) { - -} - -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); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "hello", + "!hello", + "", + "514-555", + "byebye" +] + +let outputs = [ + "olleh", + "olleh!", + "", + "555-415", + "eybeyb" +] + +/* +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 rev = str.split(''); + var n = 0; + for (var i = str.length-1; i > -1; i--) { + rev[n] = str[i]; + n++; + } + rev = rev.join(''); + return rev; +} + +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/problems/problem9.js b/problems/problem9.js index 5c52ef5..3a94031 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -1,32 +1,46 @@ -var assert = require('assert'); - -// we need 5 test cases. -let inputs = [ - -] - -let outputs = [ - -] - -/* -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 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); - +var assert = require('assert'); + +// we need 5 test cases. +let inputs = [ + "say hello", + "hello", + "", + "4344 32 2 52", + "I am 56 years old" +] + +let outputs = [ + 'hello', + 'hello', + '', + '4344', + 'years' +] + +/* +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 long = ''; + if (str == "") {return "";} + str = str.split(' '); + for (var i=0; i= long.length) {long = str[i];} + } + return long; +} + +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); +