Skip to content

Commit c90fb01

Browse files
committed
Solutions for Ex 103 and Ex104
1 parent 18faaec commit c90fb01

File tree

7 files changed

+76
-30
lines changed

7 files changed

+76
-30
lines changed

Ex103/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Ex 103</title>
7+
<h1>Excercise 103</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to find the maximum number of a given positive integer by deleting exactly one digit of the given number.
13+
</p>
14+
15+
</body>
16+
</html>

Ex103/index.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
// declare function with a given string as the parameter
22

3-
const build_palindrome = (str) => {
4-
3+
const digit_delete = (num) => {
4+
let result = 0, // variable to store maximum resulting number
5+
num_digits = []; // Array to store individual digits of the number
6+
7+
// Extract digits of the number and store them in an array
8+
while(num){
9+
num_digits.push(num % 10);
10+
num = Math.floor(num / 10);
11+
}
12+
// Iterate through each digit and generate numbers after deleting each digit
13+
for(let i = 0; i < num_digits.length; i++){
14+
let temp = 0; // Temporary variable to store generated number
15+
16+
// Create a new number without the current indexed digit
17+
for(let j = num_digits.length - 1; j >= 0; j--){
18+
if(j !== i) {
19+
temp = temp * 10 + num_digits[j];
20+
}
21+
}
22+
23+
// Store the maximum number obtained after deleting each digit
24+
result = Math.max(temp, result);
25+
}
26+
27+
return result;
528
};
629

730

831

9-
console.log(build_palindrome());
32+
console.log(digit_delete(123));

Ex104/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Ex 104</title>
7+
<h1>Excercise 104</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to find two elements of an array such that their absolute difference is not larger than a given integer. <br>
13+
However, it is as close as possible to the integer.
14+
</p>
15+
16+
</body>
17+
</html>

Ex104/index.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
// Function to build a palindrome from a given string
2-
const build_Palindrome = (new_str) => {
3-
let flag; // Variable to check if a palindrome is found
4-
for (let i = new_str.length;; i++) { // Infinite loop starting from the length of the input string
5-
flag = true; // Reset the flag for each iteration
6-
for (let j = 0; j < i - j - 1; j++) {
7-
// Check if the characters symmetrically positioned around the center are equal
8-
if (i - j - 1 < new_str.length && new_str[j] !== new_str[i - j - 1]) {
9-
flag = false; // Set the flag to false if not equal
10-
break; // Break the inner loop
1+
// Function with an array and a given number as the parameters
2+
const different_values = (arr, n) => {
3+
let max_value = -1; // initializing max value as -1
4+
for(let i = 0; i < arr.length; i++){
5+
// nested loop for comparison with subsequent elements
6+
for(let j = i + 1; j < arr.length; j++){
7+
let x = Math.abs(arr[i] - arr[j]); // Calculating absolute difference
8+
if(x <= n){ // checking if absolute difference is smaller than n
9+
max_value = Math.max(max_value,x); // Updating the maximum value if the condition is met
1110
}
1211
}
13-
if (flag) {
14-
// If a palindrome is found, complete the palindrome by adding the remaining characters
15-
for (let j = new_str.length; j < i; j++) {
16-
new_str += new_str[i - j - 1];
17-
}
18-
return new_str; // Return the palindrome
19-
}
2012
}
21-
}
13+
return max_value; // Returning max value
14+
}
2215

23-
console.log(build_Palindrome("abcddc")); // Example usage
24-
console.log(build_Palindrome("122")); // Example usage
16+
console.log(different_values([12,10,33,34], 10));

Ex57/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>Ex 56</title>
7-
<h1>Excercise 56</h1>
6+
<title>Ex 57</title>
7+
<h1>Excercise 57</h1>
88
<script src="index.js"></script>
99
</head>
1010
<body>
1111
<p>
12-
Write a JavaScript program to divide two positive numbers and return the result as string with properly formatted commas.
12+
Write a JavaScript program to create one string of specified copies (positive numbers) of a given string.
1313
</p>
1414

1515
</body>

Ex59/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ <h1>Excercise 59</h1>
99
</head>
1010
<body>
1111
<p>
12-
Write a JavaScript program to create an updated string of 4 copies of the last 3 characters of a given original string.<br>
13-
The string length must be 3 and above.
12+
Write a JavaScript program to extract the first half of a even string.
1413
</p>
1514

1615
</body>

Ex60/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ <h1>Excercise 60</h1>
99
</head>
1010
<body>
1111
<p>
12-
Write a JavaScript program to create an updated string of 4 copies of the last 3 characters of a given original string.<br>
13-
The string length must be 3 and above.
12+
Write a JavaScript program to create a new string without the first and last characters of a given string.
1413
</p>
1514

1615
</body>

0 commit comments

Comments
 (0)