From 7bc13f136c434ab5f64e974414b70bccfcb75113 Mon Sep 17 00:00:00 2001 From: MattSaeeda <43393279+MattSaeeda@users.noreply.github.com> Date: Thu, 4 Oct 2018 21:28:55 -0400 Subject: [PATCH] Add files via upload answers to test 3 --- test3-1.js | 131 +++++++++++++++++++++++++++++++++++++++ test3-2.js | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test3-3.js | 96 ++++++++++++++++++++++++++++ 3 files changed, 406 insertions(+) create mode 100644 test3-1.js create mode 100644 test3-2.js create mode 100644 test3-3.js diff --git a/test3-1.js b/test3-1.js new file mode 100644 index 0000000..f6ae8b0 --- /dev/null +++ b/test3-1.js @@ -0,0 +1,131 @@ +## LAB TEST 3 + +/* 1. Write a JavaScript function to check whether an `input` is an array or not. +Test Data : +console.log(is_array('w3resource')); +console.log(is_array([1, 2, 4, 0])); +false +true */ + +function is_array (array1) { + +if ( Array.isArray(array1) ) +return true; + +else returrn false; +} + +/* 2. Write a JavaScript function to clone an array. +Test Data : +console.log(array_Clone([1, 2, 4, 0])); +console.log(array_Clone([1, 2, [4, 0]])); +[1, 2, 4, 0] +[1, 2, [4, 0 */]] + +function array_clone ( existingArray[]) { + + cloneArray = existingArray; + + return cloneArray; +} + +/* 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])); +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)); +Expected Output : +7 +[] +[7, 9, 0] +[7, 9, 0, -2] +[] */ + +function first (array1 , n ) { + + console.log( array1.splice(0 , n ); +} + +/* 4. Write a JavaScript function to get the last element of an array. Passing a parameter 'n' will return the last 'n' elements of the array. +Test Data : +console.log(last([7, 9, 0, -2])); +console.log(last([7, 9, 0, -2],3)); +console.log(last([7, 9, 0, -2],6)); +Expected Output : +-2 +[9, 0, -2] +[7, 9, 0, -2] + */ + +function last (array1[]){ + + return array1.pop(); +} + + +/* 5. Write a simple JavaScript program to join all elements of the following array into a string. +Sample array : myColor = ["Red", "Green", "White", "Black"]; +Expected Output : +"Red,Green,White,Black" +"Red,Green,White,Black" +"Red+Green+White+Black" + */ +var myColor = ["Red", "Green", "White", "Black"]; + + console.log( myColor.join() ); + + console.log( myColor.join() ); + + console.log( myColor.join('+') ); + + +/* 6. Write a JavaScript program which accept a number as input and insert dashes (-) between each two even numbers. For example if you accept 025468 + the output should be 0-254-6-8. */ + +function testNum (x) { + + var digits = (x).toString(10).split("").map(Number); + + var newDigit[]; + + for (i = 0 , i >= digits.lenght , i++ ) { + + +/* 7. Write a JavaScript program to sort the items of an array. +Sample array : var arr1 = [ 3, 8, 7, 6, 5, -4, 3, 2, 1 ]; +Sample Output : -4,-3,1,2,3,5,6,7,8 */ + +function sortA (array1[]) { + + console.log(array1.sort(function(a, b){return a-b})); +} + + +/* 8. Write a JavaScript program to find the most frequent item of an array. +Sample array : var arr1=[3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3]; +Sample Output : a ( 5 times ) */ + +function mostFrequent (array1[]){ +; +var mf = 1; +var m = 0; +var item; +for (var i=0; i36) + return 'Base between 2 and 36'; + + return parseInt(number + '', initial_base) + .toString(change_base); + } + +/* 2. Write a JavaScript function to convert a binary number to a decimal number. +Test Data : +console.log(bin_to_dec('110011')); +console.log(bin_to_dec('100')); +51 +4 */ + +function btod ( i ) { +console.log(i); +} + +/* 3. Write a JavaScript function to convert a decimal number to binary, hexadecimal or octal number. +Test Data : +console.log(dec_to_bho(120,'B')); +console.log(dec_to_bho(120,'H')); +console.log(dec_to_bho(120,'O')); +"1111000" +"78" +"170" */ + + + function dec_to_bho(number, change_base) { + if (change_base == B ){ + return parseInt(number + '', 10) + .toString(2);} else + if (change_base == H ){ + return parseInt(number + '', 10) + .toString(8);} else + if (change_base == O ){ + return parseInt(number + '', 10) + .toString(8);} else + console.log("pick H, B, or O"); + + } + + + + +/* 4. Write a JavaScript function to generate a random integer. +Test Data : +console.log(rand(20,1)); +console.log(rand(1,10)); +console.log(rand(6)); +console.log(rand()); +15 +5 +1 +0 */ + +function rand(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; + } + +/* 5. Write a JavaScript function to format a number up to specified decimal places. +Test Data : +console.log(decimals(2.100212, 2)); +console.log(decimals(2.100212, 3)); +console.log(decimals(2100, 2)); +"2.10" +"2.100" +"2100.00" */ + +function decimals(n, d) { + if ((typeof n !== 'number') || (typeof d !== 'number')) + return false; + n = parseFloat(n) || 0; + return n.toFixed(d); + } + +/* 6. Write a JavaScript function to find the highest value in an array. +Test Data : +console.log(max([12,34,56,1])); +console.log(max([-12,-34,0,-56,-1])); +56 +0 */ + +function max1(array1){ + +return Math.max(...array1); + +} + +/* 7. Write a JavaScript function to find the lowest value in an array. +Test Data : +console.log(min([12,34,56,1])); +console.log(min([-12,-34,0,-56,-1])); +1 +-56 */ + +function min1(array1){ + + return Min.max(...array1); + + } + + +/* 8. Write a JavaScript function to find the GCD (greatest common divisor) of more than 2 integers. +Test Data : +console.log(gcd_more_than_two_numbers([3,15,27])); +console.log(gcd_more_than_two_numbers([5,10,15,25])); +Output : +3 +5 */ + +function gcd_more_than_two_numbers(input) { + if (toString.call(input) !== "[object Array]") + return false; + var len, a, b; + len = input.length; + if ( !len ) { + return null; + } + a = input[ 0 ]; + for ( var i = 1; i < len; i++ ) { + b = input[ i ]; + a = gcd_two_numbers( a, b ); + } + return a; + } + + function gcd_two_numbers(x, y) { + if ((typeof x !== 'number') || (typeof y !== 'number')) + return false; + x = Math.abs(x); + y = Math.abs(y); + while(y) { + var t = y; + y = x % y; + x = t; + } + return x; + } + +/* 9. Write a JavaScript function to get the least common multiple (LCM) of more than 2 integers. +Test Data : +console.log(lcm_more_than_two_numbers([100,90,80,7])); +console.log(lcm_more_than_two_numbers([5,10,15,25])); +Output : +25200 +150 */ + +function lcm_more_than_two_numbers(input_array) { + if (toString.call(input_array) !== "[object Array]") + return false; + var r1 = 0, r2 = 0; + var l = input_array.length; + for(i=0;i 1) { + return (split_names[0] + " " + split_names[1].charAt(0) + "."); + } + return split_names[0]; +} + + +/* 6. Write a JavaScript function to hide email addresses to protect from unauthorized user. +Test Data : +console.log(protect_email("robin_singh@example.com")); +"robin...@example.com" */ + +protect_email = function (user_email) { + var avg, splitted, part1, part2; + splitted = user_email.split("@"); + part1 = splitted[0]; + avg = part1.length / 2; + part1 = part1.substring(0, (part1.length - avg)); + part2 = splitted[1]; + return part1 + "...@" + part2; +} + +/* 7. Write a JavaScript function to capitalize the first letter of each word in a string. +Test Data : +console.log(capitalize_Words('js string exercises')); +"Js String Exercises" + */ + +function capitalize_words(str) +{ + str = str.split(" "); + + for (var i = 0, x = str.length; i < x; i++) { + str[i] = str[i][0].toUpperCase() + str[i].substr(1); + } + + return str.join(" "); +} \ No newline at end of file