diff --git a/problems/.DS_Store b/problems/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/problems/.DS_Store differ diff --git a/problems/problem1.js b/problems/problem1.js index 6d7505c..5f39b66 100644 --- a/problems/problem1.js +++ b/problems/problem1.js @@ -2,17 +2,34 @@ var assert = require('assert'); // we need 5 test cases. I provided 1 input let inputs = [ - "" + "", + "hello", + "abc", + "123", + "cat" ] let outputs = [ + undefined, + "h", + "a", + "1", + "c" ] -// 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 letter of the string that is passed to it. +// If the string does not have a first letter, return undefined + function f(str) { - -} + if (typeof str !== "string"){ + return undefined; + } + else { + return str[0]; + } + }; + function runTest(i) { var expected = outputs[i]; diff --git a/problems/problem10.js b/problems/problem10.js index e7eddde..71e36e2 100644 --- a/problems/problem10.js +++ b/problems/problem10.js @@ -1,24 +1,43 @@ -var assert = require('assert'); + var assert = require('assert'); // we need 5 test cases. let inputs = [ +"hello WORLD", +"hi there", +'BONJOUR', +"what THE F", +"ok OK OK" ] let outputs = [ + "Hello World", + "Hi There", + "Bonjour", + "What The F", + "Ok Ok Ok" ] /* -Make this function return the input string, capitalized. You must use a for loop. For example: +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 f(str){ + tempArray= str.toLowerCase().split(' '); + for (let i=0; i inputs.length) throw new Error("You do not have enough test cases"); diff --git a/problems/problem11.js b/problems/problem11.js index a9db8bc..5a9b49a 100644 --- a/problems/problem11.js +++ b/problems/problem11.js @@ -2,20 +2,40 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ + [1,1,1], + [], + [1,"cat",1], + [2,3,4], + [true,"cat",1] ] let outputs = [ + 3, + 0, + 2, + 9, + 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. +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 (let i=0; i< arr.length; i ++){ + if (typeof arr[i] === "number"){ + sum += arr[i]; + } + } + return sum; } + + 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..665ef9b 100644 --- a/problems/problem12.js +++ b/problems/problem12.js @@ -2,12 +2,22 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ + [[0,1,2,3],[1,3,4,5]], + [[3,4,22],[22,6,4]], + [[10,10,3],2], + [[2,2,2],[2,2,2]], + [[1,4,3],[1,3,3]] -] +]; let outputs = [ + [0,2,4,5], + [3,4,6], + [2,3], + [], + [4] -] +]; /* Make this function return the elements that are unique to array1 and array2. @@ -19,16 +29,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 */ + +/* +member(x,lst) returns true if and only if x is an element of lst +*/ + +function member (x, lst){ + for (let i =0; i< lst.length; i ++ ){ + if (lst[i]=== x) {return true} + } + return false +}; function f(arr1, arr2) { + uniArr = []; + for (let i = 0;i inputs.length) throw new Error("You do not have enough test cases"); var expected = outputs[i]; - var actual = f(inputs[i]); + var input = inputs[i]; + var actual = f(input[0], input[1]); assert.deepEqual(actual, expected); -} +}; runTest(0); runTest(1); diff --git a/problems/problem13.js b/problems/problem13.js index 90669e3..6326c1b 100644 --- a/problems/problem13.js +++ b/problems/problem13.js @@ -2,20 +2,37 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ + "racecAR", + "car", + "ogopogo", + "loop", + "Hanah" ] let outputs = [ + true, + false, + 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. +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 lowStr = str.toLowerCase(); + if(lowStr === lowStr.split('').reverse().join('')){ + return true + }else {return false}; } diff --git a/problems/problem14.js b/problems/problem14.js index eb49b73..93a48a8 100644 --- a/problems/problem14.js +++ b/problems/problem14.js @@ -11,7 +11,8 @@ 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. +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: diff --git a/problems/problem2.js b/problems/problem2.js index d54ae74..30ea457 100644 --- a/problems/problem2.js +++ b/problems/problem2.js @@ -2,17 +2,33 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ + "", + "hello", + "abc", + "123", + "cat" ] let outputs = [ + undefined, + "o", + "c", + "3", + "t" ] // 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 (typeof str !== "string"){ + return undefined + }else { + return str[str.length -1] + } + } -} + function runTest(i) { var expected = outputs[i]; diff --git a/problems/problem3.js b/problems/problem3.js index 11358c6..a6ed770 100644 --- a/problems/problem3.js +++ b/problems/problem3.js @@ -3,20 +3,37 @@ var assert = require('assert'); // we need 7 test cases. I've provided 2. let inputs = [ [2, 4], - [-3, 3] + [-3, 3], + ['a', 3], + [4], + [9, 11], + [2, 2], + [1, 1] ] let outputs = [ 6, - 0 + 0, + undefined, + undefined, + 20, + 4, + 2 ] /* -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. +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 (typeof x[0] !== "number" || typeof x[1] !== "number"){ + return undefined + } else {return x[0]+ x[1]} + + } + + function runTest(i) { var expected = outputs[i]; diff --git a/problems/problem4.js b/problems/problem4.js index 4082082..601cb3c 100644 --- a/problems/problem4.js +++ b/problems/problem4.js @@ -3,16 +3,32 @@ var assert = require('assert'); // we need 8 test cases. I've provided the first 2 let inputs = [ ["hello", 4], - ["", 2] + ["", 2], + [1, 'hello'], + ["bonjour", 8], + [4,1], + [["abv"], 4], + ["hamburger", 5], + ['cat', -1] + ] let outputs = [ "o", + undefined, + undefined, + undefined, + undefined, + undefined, + "r", undefined + + ] /* -Make this function return the letter at the specified position in the string. If no such letter exists, it should return 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 @@ -21,9 +37,17 @@ f("abc", 0); // a */ function f(str, index) { - + if (typeof str !== "string" || typeof index !== "number"){ + return undefined + }else if ((index < str.length === false) || (index < 0 === true)){ + return undefined + }else { + return str[index]; + } } + + function runTest(i) { var expected = outputs[i]; var input = inputs[i]; diff --git a/problems/problem5.js b/problems/problem5.js index b1e2e44..c9d05b0 100644 --- a/problems/problem5.js +++ b/problems/problem5.js @@ -2,25 +2,53 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - [2, 7] + [2, 7], + [-1, 4], + ["", 1], + [5], + [{}, 7] ] let outputs = [ - 14 + 14, + -4, + 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. +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, y) { + if (typeof x !== "number" || typeof y !== "number"){ + 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); + + +// } + 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]; + // Fixed Assertion Args (next 2 lines) : + var input = inputs[i]; + var actual = f(input[0], input[1]); + // Previous Assertion Args (which were not originally functional) : + // var actual = f(inputs[i]); */ + assert.deepEqual(actual, expected); } runTest(0); @@ -28,3 +56,6 @@ runTest(1); runTest(2); runTest(3); runTest(4); + + + diff --git a/problems/problem6.js b/problems/problem6.js index d31ae17..faaa50e 100644 --- a/problems/problem6.js +++ b/problems/problem6.js @@ -4,16 +4,27 @@ var assert = require('assert'); // we need 6 test cases. let inputs = [ ["add", 10, 20], - ["chair", 20, 10] -] + ["chair", 20, 10], + ["mult", 10, 20], + ["sub", 10, 20], + ["hello", 6, 6], + ["sub", 10, {}] +]; let outputs = [ - 30 + 30, + undefined, + 200, + -10, + 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. +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 @@ -22,16 +33,30 @@ f("spoof", 10, 10); // undefined */ function f(operation, firstArgument, secondArgument) { - -} - -function runTest(i) { + if (typeof firstArgument !== "number"|| typeof secondArgument !== "number"){ + return undefined + }else if (operation === "add"){ + return firstArgument + secondArgument + }else if (operation === "sub"){ + return firstArgument - secondArgument + }else if (operation === "mult"){ + return firstArgument * secondArgument + } + 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]); + var input = inputs[i]; + var actual = f(input[0],input[1],input[2]); assert.deepEqual(actual, expected); } + + runTest(0); runTest(1); runTest(2); diff --git a/problems/problem7.js b/problems/problem7.js index c3bf4b1..79b8c8d 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -1,17 +1,36 @@ + + var assert = require('assert'); // we need 7 test cases. let inputs = [ + ["hello",3], + [3, "hello"], + ["hello",-1 ], + ["hello", 0], + [{},3], + ["hello",{}], + ["bye",2] + ] let outputs = [ + "hellohellohello", + undefined, + "", + "", + undefined, + undefined, + "byebye", + ] /* 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. +If a negative number or zero is specified, return an empty string. +If any invalid parameters are supplied return undefined. For example: @@ -20,13 +39,21 @@ f("fo", 3) // "fofofo" f("foo", -1) // undefined */ function f(str, n) { - -} + if(typeof n !== "number" || typeof str !== "string"){ + return undefined + }else if (n<=0 === true){ + return "" + }else + {let temp = ""; + for (let i =0; 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]); assert.deepEqual(actual, expected); } diff --git a/problems/problem8.js b/problems/problem8.js index 6165932..93ee109 100644 --- a/problems/problem8.js +++ b/problems/problem8.js @@ -2,18 +2,34 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ + "hello", + "cat", + "debug", + "pizza time", + "here we go" ] let outputs = [ + "olleh", + "tac", + "gubed", + "emit azzip", + 'og ew ereh' ] /* -Make this function return the input string, reversed. For example "hello" would return "olleh" and "how are you" would return "uoy era woh". +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) { + let temp = ""; + for (let i = str.length-1; i>=0; i --){ + temp += str[i] + } + return temp; } diff --git a/problems/problem9.js b/problems/problem9.js index 5c52ef5..aca7bf6 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -2,18 +2,47 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ + "hi hello", + "", + "hi yo", + "bonjour hi", + "oh hello megan" ] let outputs = [ + "hello", + "", + "yo", + "bonjour", + "megan" ] /* -Make this function return the longest word in the input string. If the input string is empty then return an empty string. +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) { + if(typeof str !== "string"){ + return "" + } + var tempArray = str.split(' '); + var bigLength =0; + var longestWord = ""; + + for (let i = tempArray.length-1; i>=0; i--){ + if (tempArray[i].length > bigLength){ + bigLength= tempArray[i].length; + longestWord = tempArray[i]; + + } + + }return longestWord }