From 09ac20a875b64ac245608e7e682f21533dbbfc28 Mon Sep 17 00:00:00 2001 From: mamouel Date: Wed, 10 Jan 2018 13:42:35 -0500 Subject: [PATCH 1/4] =?UTF-8?q?probl=C3=A8me=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/problem1.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/problems/problem1.js b/problems/problem1.js index 6d7505c..b619c43 100644 --- a/problems/problem1.js +++ b/problems/problem1.js @@ -2,8 +2,14 @@ var assert = require('assert'); // we need 5 test cases. I provided 1 input let inputs = [ - "" -] + "", + 2, + "hello", + "world!", + 25 +]; + +console.log(inputs) let outputs = [ @@ -11,7 +17,7 @@ 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) { - + return str[0]; } function runTest(i) { From ae24759fc9b5fb6131d042aba813b7936524f177 Mon Sep 17 00:00:00 2001 From: mamouel Date: Wed, 10 Jan 2018 17:05:42 -0500 Subject: [PATCH 2/4] =?UTF-8?q?exo=201=20=C3=A0=209?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/problem1.js | 16 ++++++++++------ problems/problem2.js | 24 +++++++++++++++++------- problems/problem3.js | 19 +++++++++++++++---- problems/problem4.js | 19 ++++++++++++++++--- problems/problem5.js | 17 +++++++++++++---- problems/problem6.js | 28 ++++++++++++++++++++++++---- problems/problem7.js | 28 +++++++++++++++++++++++----- problems/problem8.js | 22 +++++++++++++++++++--- problems/problem9.js | 26 ++++++++++++++++++++++---- 9 files changed, 159 insertions(+), 40 deletions(-) diff --git a/problems/problem1.js b/problems/problem1.js index b619c43..bb4698e 100644 --- a/problems/problem1.js +++ b/problems/problem1.js @@ -3,20 +3,24 @@ var assert = require('assert'); // we need 5 test cases. I provided 1 input let inputs = [ "", - 2, - "hello", - "world!", - 25 + "abc", + "cba", + "123", + "1a2" ]; -console.log(inputs) let outputs = [ - + undefined, + "a", + "c", + "1", + "1" ] // 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(str.length == 0) return undefined; return str[0]; } diff --git a/problems/problem2.js b/problems/problem2.js index d54ae74..2d5147b 100644 --- a/problems/problem2.js +++ b/problems/problem2.js @@ -2,16 +2,26 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - -] - -let outputs = [ + "", + "abc", + "cba", + "123", + "1a2" + ]; + -] + let outputs = [ + undefined, + "c", + "a", + "3", + "2" + ] // 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(str.length == 0) return undefined; + return str[str.length-1]; } function runTest(i) { @@ -24,4 +34,4 @@ runTest(0); runTest(1); runTest(2); runTest(3); -runTest(4); +runTest(4); \ No newline at end of file diff --git a/problems/problem3.js b/problems/problem3.js index 11358c6..2fe605a 100644 --- a/problems/problem3.js +++ b/problems/problem3.js @@ -3,19 +3,30 @@ var assert = require('assert'); // we need 7 test cases. I've provided 2. let inputs = [ [2, 4], - [-3, 3] + [-3, 3], + [-10,2], + [-3, 9], + [-5, 6], + [4,13], + [1, 3] ] let outputs = [ 6, - 0 + 0, + -8, + 6, + 1, + 17, + 4 ] /* 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(arr) { + if(arr[0] == undefined || arr[1] == undefined || arr[0] == NaN || arr[1] == NaN) return undefined; + return arr[0]+arr[1]; } function runTest(i) { diff --git a/problems/problem4.js b/problems/problem4.js index 4082082..6889238 100644 --- a/problems/problem4.js +++ b/problems/problem4.js @@ -3,12 +3,24 @@ var assert = require('assert'); // we need 8 test cases. I've provided the first 2 let inputs = [ ["hello", 4], - ["", 2] + ["", 2], + ["world", 3], + ["hello", 5], + ["hello", 1], + ["javascript", 7], + ["javascript", 11], + ["javascript", 0] ] let outputs = [ "o", - undefined + undefined, + "l", + undefined, + "e", + "i", + undefined, + "j" ] /* @@ -21,7 +33,8 @@ f("abc", 0); // a */ function f(str, index) { - + return str[index] + if (str[index] === undefined) return undefined } function runTest(i) { diff --git a/problems/problem5.js b/problems/problem5.js index b1e2e44..e709fb4 100644 --- a/problems/problem5.js +++ b/problems/problem5.js @@ -2,18 +2,27 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - [2, 7] + [2, 7], + [2, 18], + [5, 7], + [-2, 22], + [2,6], ] let outputs = [ - 14 + 14, + 36, + 35, + -44, + 12 ] /* 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(arr) { + if(arr[0] == undefined || arr[1] == undefined || arr[0] == NaN || arr[1] == NaN) return undefined; + return arr[0]*arr[1]; } function runTest(i) { diff --git a/problems/problem6.js b/problems/problem6.js index d31ae17..d7541d7 100644 --- a/problems/problem6.js +++ b/problems/problem6.js @@ -4,11 +4,20 @@ var assert = require('assert'); // we need 6 test cases. let inputs = [ ["add", 10, 20], - ["chair", 20, 10] + ["mult", 20, 10], + ["add", 10, 200], + ["sub", 20, 10], + ["sub", 10, 20], + ["spoof", 20, 10], ] let outputs = [ - 30 + 30, + 200, + 210, + 10, + -10, + undefined ] /* @@ -21,10 +30,17 @@ f("mult", 2, 3); // 6 f("spoof", 10, 10); // undefined */ -function f(operation, firstArgument, secondArgument) { - +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]; @@ -38,3 +54,7 @@ runTest(2); runTest(3); runTest(4); runTest(5); + + + + diff --git a/problems/problem7.js b/problems/problem7.js index c3bf4b1..0f58293 100644 --- a/problems/problem7.js +++ b/problems/problem7.js @@ -2,11 +2,23 @@ var assert = require('assert'); // we need 7 test cases. let inputs = [ - + ["foo", 3], + ["bo", 6], + ["", 3], + ["foo", -1], + ["hooo", 2], + ["foo", -3], + ["b", 5] ] let outputs = [ - + "foofoofoo", + "bobobobobobo", + undefined, + undefined, + "hooohooo", + undefined, + "bbbbb" ] /* @@ -19,14 +31,20 @@ f("foo", 3) // "foofoofoo" f("fo", 3) // "fofofo" f("foo", -1) // undefined */ -function f(str, n) { - +function f(str,n) { + if(n <= 0 || str === "") return undefined; + var newStr = ""; + for (var i=0; i < n; i++) { + newStr += str + } + return newStr; } + 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][0],inputs[i][1]); assert.deepEqual(actual, expected); } diff --git a/problems/problem8.js b/problems/problem8.js index 6165932..e5fc2c3 100644 --- a/problems/problem8.js +++ b/problems/problem8.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "hello", + "world", + "how are you", + "hello there", + "kayak" ] let outputs = [ - + "olleh", + "dlrow", + "uoy era woh", + "ereht olleh", + "kayak" ] /* @@ -14,9 +22,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 = ""; + for (var i=str.length-1; i >=0; i--) { + newStr += str[i] + } + return newStr; } +//function f(str) { +// return str.split("").reverse().join("") +//} + 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/problem9.js b/problems/problem9.js index 5c52ef5..9eb3d59 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -2,19 +2,37 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "you are nice", + "welcome to my country", + "hello mate", + "how are you going", + "" ] let outputs = [ - + "nice", + "welcome", + "hello", + "going", + "" ] /* 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 f(string) { + if (string === "") return ""; + var str = string.split(" "); + var longest = 0; + var word = ""; + for (var i = 0; i < str.length; i++) { + if (longest < str[i].length) { + longest = str[i].length; + word = str[i]; + } + } + return word; } function runTest(i) { From 6bf558c8d5c099fad429cc7f4c6728b89523d5da Mon Sep 17 00:00:00 2001 From: mamouel Date: Wed, 10 Jan 2018 19:51:44 -0500 Subject: [PATCH 3/4] ajout des exos 10, 11 et 13 --- problems/problem10.js | 19 ++++++++++++++++--- problems/problem11.js | 23 ++++++++++++++++++++--- problems/problem13.js | 18 ++++++++++++++---- problems/problem9.js | 8 ++++---- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/problems/problem10.js b/problems/problem10.js index e7eddde..6128a22 100644 --- a/problems/problem10.js +++ b/problems/problem10.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "hello world", + "ALL YOUR BASE ARE BELONG", + "jAvAscrIpt is FuN", + "ABBBaaach nbxjHH", + "wELComE" ] let outputs = [ - + "Hello World", + "All Your Base Are Belong", + "Javascript Is Fun", + "Abbbaaach Nbxjhh", + "Welcome" ] /* @@ -17,7 +25,12 @@ f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong */ function f(str) { - + var arr = str.toLowerCase().split(" "); + var newStr = ""; + for (var i = 0; i < arr.length; i++) { + arr[i] = arr[i][0].toUpperCase()+arr[i].slice(1); + } + return newStr += arr.join(" "); } function runTest(i) { diff --git a/problems/problem11.js b/problems/problem11.js index a9db8bc..d82aba5 100644 --- a/problems/problem11.js +++ b/problems/problem11.js @@ -2,20 +2,37 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + [2, 7, 10], + [2, 9, 1, 18], + [5, 6, 7], + [-2, 22, -3], + [], ] let outputs = [ - + 19, + 30, + 18, + 17, + 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(arr.length === 0) return 0; + var result=0; + for (var i = 0; i < arr.length; i++) { + if(!isNaN(arr[i])) { + result += arr[i] + } + + } + return result; } + 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/problem13.js b/problems/problem13.js index 90669e3..fa85b5e 100644 --- a/problems/problem13.js +++ b/problems/problem13.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + "RADAR", + "kayak", + "javascript", + "welcome", + "hello" ] let outputs = [ - + true, + true, + false, + false, + false ] /* @@ -16,8 +24,10 @@ RADAR -> Yes JAVASCRIPT -> No */ function f(str) { - -} + var cleanStr = str.replace(/[^a-z]/ig, "").toLowerCase(); + var invertedStr = cleanStr.split("").reverse().join(""); + return cleanStr === invertedStr; +} function runTest(i) { if(i > inputs.length) throw new Error("You do not have enough test cases"); diff --git a/problems/problem9.js b/problems/problem9.js index 9eb3d59..e5a9cdb 100644 --- a/problems/problem9.js +++ b/problems/problem9.js @@ -4,15 +4,15 @@ var assert = require('assert'); let inputs = [ "you are nice", "welcome to my country", - "hello mate", + "hello there", "how are you going", "" ] let outputs = [ "nice", - "welcome", - "hello", + "country", + "there", "going", "" ] @@ -27,7 +27,7 @@ function f(string) { var longest = 0; var word = ""; for (var i = 0; i < str.length; i++) { - if (longest < str[i].length) { + if (longest <= str[i].length) { longest = str[i].length; word = str[i]; } From 8634e970be15758ec6b9afbdee96a96055c8e2f7 Mon Sep 17 00:00:00 2001 From: mamouel Date: Wed, 10 Jan 2018 20:47:49 -0500 Subject: [PATCH 4/4] =?UTF-8?q?exo=2012=20pas=20termin=C3=A9=20(doesn't=20?= =?UTF-8?q?work)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/problem12.js | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/problems/problem12.js b/problems/problem12.js index 2947d1b..60dccae 100644 --- a/problems/problem12.js +++ b/problems/problem12.js @@ -2,11 +2,19 @@ var assert = require('assert'); // we need 5 test cases. let inputs = [ - + [[0,1,2,3], [1,3,4,5]], + [[0,3], [1,3,4,5]], + [[0,1,2,3], [4,5]], + [[0,1,2,5], [1,5]], + [[0,3,6], [1,3,4,5,6]] ] let outputs = [ - + [0,2,4,5], + [0,1,4,5], + [], + [0,2], + [0,1,4,5] ] /* @@ -20,8 +28,28 @@ uniqueElements([1,2,3], [1,2,3]); // [] uniqueElements(2,3); // undefined, not arrays */ function f(arr1, arr2) { - -} + var arr3 = []; + for(var i = 0; i < arr1.length; i++){ + var match = false; + console.log(arr1[i]) + for(var j = 0; j < arr2.length; j++) { + if (arr1[i] === arr2[j]) { + match = true; + } + } + if (!match) { + arr3.push(arr1[i]) + } + } + return arr3; + } + + function ff(arr1, arr2) { + var out1 = f(arr1, arr2); + var out2 = f(arr2, arr1); + return out1.concat(out2) + } + function runTest(i) { if(i > inputs.length) throw new Error("You do not have enough test cases");