From bbeee054780cc2d1a15b58bb577a3589d075dd55 Mon Sep 17 00:00:00 2001 From: Megan McEwan Date: Wed, 10 Jan 2018 21:49:36 -0500 Subject: [PATCH 1/4] day 3 problems round 1 --- problems/.DS_Store | Bin 0 -> 6148 bytes problems/problem1.js | 25 ++++++++++++++++++---- problems/problem10.js | 8 ++++++- problems/problem2.js | 18 +++++++++++++++- problems/problem3.js | 29 ++++++++++++++++++++------ problems/problem4.js | 30 ++++++++++++++++++++++++--- problems/problem5.js | 47 +++++++++++++++++++++++++++++++++++------- problems/problem6.js | 38 +++++++++++++++++++++++++++------- problems/problem7.js | 35 +++++++++++++++++++++++++++---- problems/problem8.js | 18 +++++++++++++++- problems/problem9.js | 31 +++++++++++++++++++++++++++- 11 files changed, 243 insertions(+), 36 deletions(-) create mode 100644 problems/.DS_Store diff --git a/problems/.DS_Store b/problems/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0tempArray.length; i ++){ + + } + } 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..d12d435 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,29 @@ f("spoof", 10, 10); // undefined */ function f(operation, firstArgument, secondArgument) { + 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) { + 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 actual = f(...inputs[i]); 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 } From 428f4a93d32a4b24146f0c66971518e007a2d460 Mon Sep 17 00:00:00 2001 From: Megan McEwan Date: Thu, 11 Jan 2018 20:28:47 -0500 Subject: [PATCH 2/4] day 3 js problems updated --- problems/ex.js | 123 ++++++++++++++++++++++++++++++++++++++++++ problems/problem10.js | 27 +++++++--- problems/problem11.js | 24 ++++++++- problems/problem12.js | 15 +++++- problems/problem13.js | 19 ++++++- problems/problem14.js | 3 +- 6 files changed, 199 insertions(+), 12 deletions(-) create mode 100644 problems/ex.js diff --git a/problems/ex.js b/problems/ex.js new file mode 100644 index 0000000..1bd0eba --- /dev/null +++ b/problems/ex.js @@ -0,0 +1,123 @@ + +// var x = 'hello wORLD'; + +// function f(str) { +// tempArray = str.split(' '); +// for (let i=0; i>tempArray.length; i ++){ +// newArr = str.split("") + +// } + +// } + + + + +// function f(str){ +// tempArray= str.toLowerCase().split(' '); +// for (let i=0; i tempArray.length; i ++){ - +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..2370ef0 100644 --- a/problems/problem12.js +++ b/problems/problem12.js @@ -19,8 +19,21 @@ 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 f(arr1, arr2) { - + uniArr=[]; + for (i=0; i < arr1.length; i ++){ + for (j=0; j 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: From d0f4f0e70dcb0fe8b095a2e1828348f4658fa838 Mon Sep 17 00:00:00 2001 From: Megan McEwan Date: Sun, 14 Jan 2018 22:57:07 -0500 Subject: [PATCH 3/4] updates to problems 6 & 12 --- problems/ex.js | 58 +++++++++++++++++++++++++++++++------------ problems/problem12.js | 53 ++++++++++++++++++++++++++++----------- problems/problem6.js | 9 ++++--- 3 files changed, 86 insertions(+), 34 deletions(-) diff --git a/problems/ex.js b/problems/ex.js index 1bd0eba..e04210f 100644 --- a/problems/ex.js +++ b/problems/ex.js @@ -103,21 +103,47 @@ If the array is empty, return zero. // console.log(f([0,1,2,3], [1,3,4,5])); -function f(array1, array2) { -var unique = []; -for(var i = 0; i < array1.length; i++){ - var found = false; - - for(var j = 0; j < array2.length; j++){ // j < is missed; - if(array1[i] == array2[j]){ - found = true; - break; - } - } - if(found == false){ - unique.push(array1[i]); +// function f(array1, array2) { +// var unique = []; +// for(var i = 0; i < array1.length; i++){ +// var found = false; + +// for(var j = 0; j < array2.length; j++){ // j < is missed; +// if(array1[i] == array2[j]){ +// found = true; +// break; +// } +// } +// if(found == false){ +// unique.push(array1[i]); +// } +// } +// } + + +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/problem6.js b/problems/problem6.js index d12d435..faaa50e 100644 --- a/problems/problem6.js +++ b/problems/problem6.js @@ -7,9 +7,9 @@ let inputs = [ ["chair", 20, 10], ["mult", 10, 20], ["sub", 10, 20], - ["hello", 6, 6] + ["hello", 6, 6], ["sub", 10, {}] -] +]; let outputs = [ 30, @@ -46,11 +46,12 @@ function f(operation, firstArgument, secondArgument) { 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); } From c23e1a5fdf0c0fa5d21864138fb40324296bf459 Mon Sep 17 00:00:00 2001 From: meganmcewan <34549727+meganmcewan@users.noreply.github.com> Date: Mon, 15 Jan 2018 21:57:41 -0500 Subject: [PATCH 4/4] Delete ex.js --- problems/ex.js | 149 ------------------------------------------------- 1 file changed, 149 deletions(-) delete mode 100644 problems/ex.js diff --git a/problems/ex.js b/problems/ex.js deleted file mode 100644 index e04210f..0000000 --- a/problems/ex.js +++ /dev/null @@ -1,149 +0,0 @@ - -// var x = 'hello wORLD'; - -// function f(str) { -// tempArray = str.split(' '); -// for (let i=0; i>tempArray.length; i ++){ -// newArr = str.split("") - -// } - -// } - - - - -// function f(str){ -// tempArray= str.toLowerCase().split(' '); -// for (let i=0; i