From a21dc9a0991f2da88e7529c3c4da54332d412df6 Mon Sep 17 00:00:00 2001 From: mehthab Date: Tue, 28 Aug 2018 15:57:05 +0530 Subject: [PATCH] task on javascript --- aler.js | 13 +++++++++++++ arraycheck.js | 11 +++++++++++ firstnelements.js | 33 +++++++++++++++++++++++++++++++++ freqelements.js | 26 ++++++++++++++++++++++++++ insertdashes.js | 20 ++++++++++++++++++++ joinelements.js | 14 ++++++++++++++ largest.js | 12 ++++++++++++ largestcondition.js | 22 ++++++++++++++++++++++ lastnelements.js | 25 +++++++++++++++++++++++++ loop.js | 10 ++++++++++ nestedloop.js | 25 +++++++++++++++++++++++++ sort.js | 4 ++++ sortcondition.js | 15 +++++++++++++++ sumproduct.js | 15 +++++++++++++++ uppercase.js | 15 +++++++++++++++ 15 files changed, 260 insertions(+) create mode 100644 aler.js create mode 100644 arraycheck.js create mode 100644 firstnelements.js create mode 100644 freqelements.js create mode 100644 insertdashes.js create mode 100644 joinelements.js create mode 100644 largest.js create mode 100644 largestcondition.js create mode 100644 lastnelements.js create mode 100644 loop.js create mode 100644 nestedloop.js create mode 100644 sort.js create mode 100644 sortcondition.js create mode 100644 sumproduct.js create mode 100644 uppercase.js diff --git a/aler.js b/aler.js new file mode 100644 index 0000000..2da81e3 --- /dev/null +++ b/aler.js @@ -0,0 +1,13 @@ +/*Write a JavaScript conditional statement to find the sign of product of three numbers. Display an alert box with the specified sign. Go to the editor +Sample numbers : 3, -7, 2 +Output : The sign is - */ + +let sign=(a,b,c)=>{ + let p=a*b*c; + if(p<0) +alert("the sign is -") + else + alert("the sign is +") +} + +sign(3,-7,2) \ No newline at end of file diff --git a/arraycheck.js b/arraycheck.js new file mode 100644 index 0000000..d6639f3 --- /dev/null +++ b/arraycheck.js @@ -0,0 +1,11 @@ +//to check an array + +let checkarray=(input)=>{ + + console.log(Array.isArray(input)); + +} + checkarray([1,2,3]); + + + diff --git a/firstnelements.js b/firstnelements.js new file mode 100644 index 0000000..5b0dce6 --- /dev/null +++ b/firstnelements.js @@ -0,0 +1,33 @@ +/*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] +[] */ + + +let first=(arr,param)=> +{ + + + if(param==null) + param=1; + if(param<0) + param=0; + console.log(arr.slice(0,param)); +} +first([7,9,0,-2]); +first([],3) +first([7,9,0,-2],3) +first([7,9,0,-2],6) +first([7,9,0,-2],-3) + + + diff --git a/freqelements.js b/freqelements.js new file mode 100644 index 0000000..eb3bb9b --- /dev/null +++ b/freqelements.js @@ -0,0 +1,26 @@ +/*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 ) +*/ + +let count=(input)=>{ + let c=0; + let item; + let max=1; + for(let i=0;imax){ + max=c; + item=input[i]; + } + } + console.log(item+"("+max+"times)"); + } + +count([3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3]); + + diff --git a/insertdashes.js b/insertdashes.js new file mode 100644 index 0000000..9c4924d --- /dev/null +++ b/insertdashes.js @@ -0,0 +1,20 @@ +//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. + +let dash=(input)=>{ + + let arr1=Array.from(input.toString()); + console.log(arr1) + let i; + let j; + for(i=0;i{ + console.log(arr1.join(',')); + console.log(arr1.join('+')); +} +join1(['red','green','blue']); + diff --git a/largest.js b/largest.js new file mode 100644 index 0000000..943cf9d --- /dev/null +++ b/largest.js @@ -0,0 +1,12 @@ +//Write a JavaScript program that accept two integers and display the larger. +let large=(a,b)=>{ + if(a>b) + console.log("largest is "+a); + else + console.log("largest is "+b) +} +large(10,15) +/* +Exception: SyntaxError: expected expression, got ')' +@Scratchpad/3:1 +*/ \ No newline at end of file diff --git a/largestcondition.js b/largestcondition.js new file mode 100644 index 0000000..a16978a --- /dev/null +++ b/largestcondition.js @@ -0,0 +1,22 @@ +/* Write a JavaScript conditional statement to sort three numbers. Display an alert box to show the result. +Sample numbers : 0, -1, 4 +Output : 4, 0, -1 */ + + + +let sign=(a,b,c,d,e)=>{ + let max; + let arr=Array.of(a,b,c,d,e); + max=arr[0]; + for(let i=0;iarr[i+1] && arr[i]>max) + max=arr[i]; + + + + alert("largest is "+max); + +} + +sign(-5,-2,-6,0,-1); + diff --git a/lastnelements.js b/lastnelements.js new file mode 100644 index 0000000..c67c13e --- /dev/null +++ b/lastnelements.js @@ -0,0 +1,25 @@ +/*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]*/ + + +let last=(arr1,param1)=>{ + if(param1==null) + param1=1; + arr1.reverse(); + let arr2=arr1.slice(0,param1); + console.log(arr2.reverse())} +last([7,9,0,-2]); +last([7,9,0,-2],3); +last([7,9,0,-2],6); + + + + + diff --git a/loop.js b/loop.js new file mode 100644 index 0000000..9248abc --- /dev/null +++ b/loop.js @@ -0,0 +1,10 @@ +let loop=()=>{ + for(let i=0;i<=15;++i){ + if(i%2==0) + console.log(i+" is even"); + else + console.log(i+" is odd") + } +} + +loop(); \ No newline at end of file diff --git a/nestedloop.js b/nestedloop.js new file mode 100644 index 0000000..6a743de --- /dev/null +++ b/nestedloop.js @@ -0,0 +1,25 @@ +/* Write a JavaScript program which prints the elements of the following array. Go to the editor +Note : Use nested for loops. +Sample array : var a = [[1, 2, 1, 24], [8, 11, 9, 4], [7, 0, 7, 27], [7, 4, 28, 14], [3, 10, 26, 7]]; +Sample Output : +"row 0" +" 1" +" 2" +" 1" +" 24" +"row 1" +------ +------ +*/ + +let nest=(input)=>{ + + for(let i=0;i{ + let arr=Array.of(a,b,c); + for(let j=0;j{ + let sum1=0; + let product=1; + for(let i=0;i