0% found this document useful (0 votes)
9 views

JS Challenges Part1

The document contains code snippets in Java, Python, and JavaScript that demonstrate various programming concepts like printing patterns, manipulating arrays, comparing arrays, filtering arrays, mathematical operations, and object instantiation.

Uploaded by

pratik raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

JS Challenges Part1

The document contains code snippets in Java, Python, and JavaScript that demonstrate various programming concepts like printing patterns, manipulating arrays, comparing arrays, filtering arrays, mathematical operations, and object instantiation.

Uploaded by

pratik raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

// Java Program to print


// Pyramid pattern
import java.util.*;

public class {
// Function to demonstrate pattern
public static void printPattern(int n)
{
int i, j;

// outer loop to handle rows


for (i = 1; i <= n; i++) {

// inner loop to handle columns


for (j = 1; j <= i; j++) {
System.out.print("*");
}

// printing new line for each row


System.out.println();
}
}

// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}

2.public class Pattern


{
public static void main(String args[]) {
for (int i = 65; i < 70; i++) {
for (int j = 65; j <= i; j++) {
if (i % 2 == 0)
System.out.print((char)(j+32));
else
System.out.print((char)j);
}
System.out.println();
}
}
}
3.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tab using javascript</title>
<!-- style of the tab-->
<style>
#tab1,
#tab2,
#tab3 {
text-align: center;
float: left;
padding: 5px 10px 5px 10px;
background: #b00098;
color: #ffffff;
margin: 0px 5px 0px 5px;
cursor: pointer;
border-radius: 5px;
}
#tab1:hover,
#tab2:hover,
#tab3:hover {
background: #ecade4;
}
#tab1Content,
#tab2Content,
#tab3Content {
width: auto;
height: 100px;
padding: 20px;
border: 1px solid #b00098;
border-radius: 10px;
}
#tab1Content,
#tab2Content,
#tab3Content {
display: none;
}
</style>
</head>
<body>
<!-- HTML Of the Tab -->
<div id="tab1" onClick="JavaScript:selectTab(1);">Tab 1</div>
<div id="tab2" onClick="JavaScript:selectTab(2);">Tab 2</div>
<div id="tab3" onClick="JavaScript:selectTab(3);">Tab 3</div>
<br />
<div id="tab1Content">This is first tab.</div>
<div id="tab2Content">This is Second tab</div>
<div id="tab3Content">This is third tab</div>
<!-- Javascript Code of the tab -->
<script>
function selectTab(tabIndex) {
//Hide All Tabs
document.getElementById("tab1Content").style.display = "none";
document.getElementById("tab2Content").style.display = "none";
document.getElementById("tab3Content").style.display = "none";
//Show the Selected Tab
document.getElementById("tab" + tabIndex + "Content").style.display =
"block";
}
</script>
</body>
</html>

4.// Java Program to Remove Duplicate Elements


// From the Array using extra space

public class Main {

public static int removeduplicates(int a[], int n)


{
if (n == 0 || n == 1) {
return n;
}

// creating another array for only storing


// the unique elements
int[] temp = new int[n];
int j = 0;

for (int i = 0; i < n - 1; i++) {


if (a[i] != a[i + 1]) {
temp[j++] = a[i];
}
}

temp[j++] = a[n - 1];

// Changing the original array


for (int i = 0; i < j; i++) {
a[i] = temp[i];
}

return j;
}
public static void main(String[] args)
{
int a[] = { 1, 1, 2, 2, 2 };
int n = a.length;

n = removeduplicates(a, n);

// Printing The array elements


for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
}
5.// Iterative java program to reverse an
// array
public class {

static void rvereseArray(int arr[],


int start, int end)
{
int temp;

while (start < end)


{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

static void printArray(int arr[],


int size)
{
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");

System.out.println();
}

// Driver code
public static void main(String args[]) {

int arr[] = {1, 2, 3, 4, 5, 6};


printArray(arr, 6);
rvereseArray(arr, 0, 5);
System.out.print("Reversed array is \n");
printArray(arr, 6);
}
}
6.function filter_array(test_array) {
var index = -1,
arr_length = test_array ? test_array.length : 0,
resIndex = -1,
result = [];

while (++index < arr_length) {


var value = test_array[index];

if (value) {
result[++resIndex] = value;
}
}

return result;
}
console.log(filter_array([ 15, false, -22, undefined, 47, null]));

7.function compareArrays(arrfirst, arrsecond) {


if (arrfirst.length !== arrsecond.length) {
return false;
}

for (let i = 0; i < arrfirst.length; i++) {


if (arrfirst[i] !== arrsecond[i]) {
return false;
}
}

return true;
}
const array1 = [3, 4, 8];
const array2 = [1, 2, 3];
const array3 = [3, 4, 8];
console.log(compareArrays(array1, array2));
console.log(compareArrays(array1, array3));

8.

9.

10.def multiply(a, b):


return a*b
# A single '/' will yield a float / partial / decimal point having number.
def divide(a, b):
return a/b

# A double '/' will yield an integer / whole number answer.


def integer_divide(a, b):
return a//b

def add(a, b):


return a+b

def subtract(a, b):


return a-b

def modulo(a, b):


return a%b

print(multiply(2,4))
print(multiply(5,3))

output
8
15

11.
1. Using new keyword.

Tester tester1 = new Tester();


2. Using Class.forName() method

Tester tester2 = (Tester)Class.forName("Tester").newInstance();


3. Using clone method.

Tester tester3 = tester1.clone();


4. Using Constructor.forName() method

Tester tester4 = Tester.class.getConstructor().newInstance();


5. Using Deserialization

ObjectInputStream objectInputStream = new ObjectInputStream(inputStream );


Tester tester5 = (MyObject) objectInputStream.readObject();

You might also like