From 098c779cec1474fec3d0fbe96f9a7b95149d02b1 Mon Sep 17 00:00:00 2001 From: Gerrits Jan Date: Tue, 2 Apr 2019 14:09:15 +0300 Subject: [PATCH] add exercise files --- students/jan-gerrits/ex1.js | 18 ++++++++++++++++++ students/jan-gerrits/ex2.js | 29 +++++++++++++++++++++++++++++ students/jan-gerrits/ex3.js | 21 +++++++++++++++++++++ students/jan-gerrits/ex4.js | 18 ++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 students/jan-gerrits/ex1.js create mode 100644 students/jan-gerrits/ex2.js create mode 100644 students/jan-gerrits/ex3.js create mode 100644 students/jan-gerrits/ex4.js diff --git a/students/jan-gerrits/ex1.js b/students/jan-gerrits/ex1.js new file mode 100644 index 0000000..d51c771 --- /dev/null +++ b/students/jan-gerrits/ex1.js @@ -0,0 +1,18 @@ +Array.isArray(input){ + return Array.isArray(input) +} + + +/*var x = [1, 2, 4, 0]; +var n = x.includes("w3resource"); +var p = x.includes(0,1,2,4); + +console.log = n; +console.log = p; + +Write a JavaScript function to check whether an input is an array or not. + +Test Data : +console.log(isArray('w3resource')); // false +console.log(isArray([1, 2, 4, 0])); // true +*/ \ No newline at end of file diff --git a/students/jan-gerrits/ex2.js b/students/jan-gerrits/ex2.js new file mode 100644 index 0000000..e7ac875 --- /dev/null +++ b/students/jan-gerrits/ex2.js @@ -0,0 +1,29 @@ +var first = function(array, n) { + if (array == null) + return void 0; + if (n == null) + return array[0]; + if (n < 0) + return []; + return array.slice(0, n); +}; + +console.log(first([7, 9, 0, -2])); +console.log(first([],3)); +console.log(first([7, 9, 0, -2],3)); +console.log(first([7, 9, 0, -2],6)); +console.log(first([7, 9, 0, -2],-3)); + + + +/* +Write a JavaScript function to get the first element of an array. Passing a parameter 'n' will return the first 'n' elements of the array. + +Test Data :  +console.log(first([7, 9, 0, -2])); // 7 +console.log(first([],3)); // [] +console.log(first([7, 9, 0, -2],3)); // [7,9,0] +console.log(first([7, 9, 0, -2],6)); // [7, 9, 0, -2] +console.log(first([7, 9, 0, -2],-3)); // [] + +*/ \ No newline at end of file diff --git a/students/jan-gerrits/ex3.js b/students/jan-gerrits/ex3.js new file mode 100644 index 0000000..fa8f2f5 --- /dev/null +++ b/students/jan-gerrits/ex3.js @@ -0,0 +1,21 @@ +function joinArray(input) { + var index = 0, + result = ''; + + while(index < input.length) + result = result + ' ' + input[index]; + index++; +} + +return result; +} +/* +Write a simple JavaScript program to join all elements of the following array into a string. + +Sample array : +var myText = ['This', 'is', 'not', 'working']; +joinArray(myText); + +Expected Output :  +This is not working + */ \ No newline at end of file diff --git a/students/jan-gerrits/ex4.js b/students/jan-gerrits/ex4.js new file mode 100644 index 0000000..e5ef4d9 --- /dev/null +++ b/students/jan-gerrits/ex4.js @@ -0,0 +1,18 @@ +function validatePIN(pin) { + if (typeof pin === "string" && !~pin.indexOf('.') && !isNaN(Number(pin)) && (pin.length === 4 || pin.length === 6)) { + return true; + } else { + return false; + } + } + + +/* +ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits. If the function is passed a valid PIN string, return true, else return false. + +validatePIN(1234); // false +validatePIN(00004324); // false +validatePIN('2312'); // true +validatePin('asd123'); // false +validatePin('000010'); // true + */ \ No newline at end of file