-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a21dc9a
Showing
15 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|
||
|