Skip to content

Commit 12a1590

Browse files
committed
Solutions for Ex 105 to Ex 107
1 parent c90fb01 commit 12a1590

File tree

6 files changed

+110
-21
lines changed

6 files changed

+110
-21
lines changed

Ex105/index.html

+17
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>

Ex105/index.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
// declare function with a given string as the parameter
1+
// declare function with a given integer as the parameter
22

3-
const build_Palindrome = (str) => {
4-
let flag; // Variable to check if a palindrome is found
5-
for (let i = str.length;; i++) { // Infinite loop starting from the length of the input string
6-
flag = true; // Reset the flag for each iteration
7-
for (let j = 0; j < i - j - 1; j++) {
8-
// Check if the characters symmetrically positioned around the center are equal
9-
if (i - j - 1 < str.length && str[j] !== str[i - j - 1]) {
10-
flag = false; // Set the flag to false if not equal
11-
break; // Break the inner loop
12-
}
13-
}
14-
if (flag) {
15-
// If a palindrome is found, complete the palindrome by adding the remaining characters
16-
for (let j = str.length; j < i; j++) {
17-
str += str[i - j - 1];
18-
}
19-
return str; // Return the palindrome
20-
}
3+
// Function to calculate the sum of digits
4+
const digitSum = (num) => {
5+
let digit_sum = 0; // initialize the sum of digits
6+
while(num){
7+
digit_sum += num % 10; // get the last digit and sum them up
8+
num = Math.floor(num / 10); // remove the last digit from the number
219
}
10+
return digit_sum;
11+
};
12+
13+
const digit_to_one = (num) => {
14+
let result = 0; // initialize count of steps to convert number
15+
while (num >= 10){ // Loop until the number becomes single digit
16+
result += 1; // increment the step count
17+
num = digitSum(num);
2218
}
19+
return result;
20+
};
21+
2322

24-
console.log(build_Palindrome("abcddc")); // Example usage
25-
console.log(build_Palindrome("122")); // Example usage
2623

24+
console.log(digit_to_one(123));
25+
console.log(digit_to_one(156));

Ex106/index.html

+17
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>

Ex106/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// declare function to divide a number by a digit
2+
const divide_digit = (num, d) => {
3+
// Check if divisor is 1, return the number itself
4+
if(d == 1){
5+
return num;
6+
}else{
7+
// Loop to divide the number by the divisor until not divisible
8+
while(num % d === 0){
9+
num /= d; // Divide the number by the divisor
10+
}
11+
return num; // Return the final divided number
12+
}
13+
}
14+
15+
16+
17+
console.log(divide_digit(-12, 2)); // Output: -3
18+
console.log(divide_digit(13, 2)); // Output: 13
19+
console.log(divide_digit(13, 1)); // Output: 13

Ex107/index.html

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 the number of sorted pairs formed by arrays of integers.<br>
13+
This is such that one element in the pair is divisible by the other one.
14+
Write a JavaScript program to find the number of sorted pairs formed by arrays of integers. This is such that one element in the pair is divisible by the other one.
15+
16+
For example - The output of [1, 3, 2] ->2 - (1,3), (1,2).
17+
The output of [2, 4, 6] -> 2 - (2,4), (2,6)
18+
The output of [2, 4, 16] -> 3 - (2,4), (2,16), (4,16)
19+
</p>
20+
21+
</body>
22+
</html>

Ex107/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// declare function with a given integer as the parameter
2+
3+
const arr_pairs = (arr) => {
4+
let result = 0;
5+
for(let i = 0; i < arr.length; i++){
6+
for(let j = i + 1; j < arr.length; j++){
7+
if(arr[i] % arr[j] === 0 || arr[j] % arr[i] === 0){
8+
result++;
9+
}
10+
}
11+
}
12+
return result;
13+
};
14+
15+
console.log(arr_pairs([1,2,3]));

0 commit comments

Comments
 (0)