Skip to content

Commit

Permalink
task on javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
mehthabt committed Aug 28, 2018
0 parents commit a21dc9a
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 0 deletions.
13 changes: 13 additions & 0 deletions aler.js
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions arraycheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//to check an array

let checkarray=(input)=>{

console.log(Array.isArray(input));

}
checkarray([1,2,3]);



33 changes: 33 additions & 0 deletions firstnelements.js
Original file line number Diff line number Diff line change
@@ -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)



26 changes: 26 additions & 0 deletions freqelements.js
Original file line number Diff line number Diff line change
@@ -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;i<input.length;++i){
c=0;
for(let j=0;j<input.length;++j){
if(input[i]==input[j]){
++c;
}}
if(c>max){
max=c;
item=input[i];
}
}
console.log(item+"("+max+"times)");
}

count([3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3]);


20 changes: 20 additions & 0 deletions insertdashes.js
Original file line number Diff line number Diff line change
@@ -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<arr1.length;++i)
{

if(arr1[i]%2==0 && arr1[i+1]%2==0)
{arr1.splice(i+1,0,'-')
}
}
console.log(arr1.join(''));
}
dash(025468);


14 changes: 14 additions & 0 deletions joinelements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*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"*/


let join1=(arr1)=>{
console.log(arr1.join(','));
console.log(arr1.join('+'));
}
join1(['red','green','blue']);

12 changes: 12 additions & 0 deletions largest.js
Original file line number Diff line number Diff line change
@@ -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
*/
22 changes: 22 additions & 0 deletions largestcondition.js
Original file line number Diff line number Diff line change
@@ -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;i<arr.length;i++)
if(arr[i]>arr[i+1] && arr[i]>max)
max=arr[i];



alert("largest is "+max);

}

sign(-5,-2,-6,0,-1);

25 changes: 25 additions & 0 deletions lastnelements.js
Original file line number Diff line number Diff line change
@@ -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);





10 changes: 10 additions & 0 deletions loop.js
Original file line number Diff line number Diff line change
@@ -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();
25 changes: 25 additions & 0 deletions nestedloop.js
Original file line number Diff line number Diff line change
@@ -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<input.length;++i){
console.log("row"+i);
for(let j=0;j<input.length;++j){

console.log(input[i][j]);

} }}
nest( [[1, 2, 1, 24], [8, 11, 9, 4], [7, 0, 7, 27], [7, 4, 28, 14], [3, 10, 26, 7]]);

4 changes: 4 additions & 0 deletions sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//Write a JavaScript program to sort the items of an array.
let arr1=[3,8,7,6,5,-4,-3,2,1];
console.log(arr1.sort());

15 changes: 15 additions & 0 deletions sortcondition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let sign=(a,b,c)=>{
let arr=Array.of(a,b,c);
for(let j=0;j<arr.length;j++)
for(let i=0;i<arr.length;i++)
if(arr[i]<arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1]
arr[i+1]=temp
}
alert(arr.join(' '))

}

sign(0,-1,4);
15 changes: 15 additions & 0 deletions sumproduct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//Write a JavaScript program to compute the sum and product of an array of integers.
let sum=(input)=>{
let sum1=0;
let product=1;
for(let i=0;i<input.length;i++)
{
sum1+=input[i];
product*=input[i];
}
console.log("sum="+sum1)
console.log("product="+product)
}

sum([2,3,5]);

15 changes: 15 additions & 0 deletions uppercase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*Write a JavaScript program which accept a string as input and swap the case of each character. For example if you input 'The Quick Brown Fox' the output should be 'tHE qUICK bROWN fOX'.*/

let str=(input)=>{
let a=Array.from(input);
for(let i=0;i<a.length;i++)
{
if(a[i]==a[i].toUpperCase())
a[i]=a[i].toUpperCase();
else
a[i]=a[i].toLowerCase();}
console.log(a.join(''));
}
str('tHE qUICK bROWN fOX')


0 comments on commit a21dc9a

Please sign in to comment.