Skip to content

Commit 1c9e4e5

Browse files
committed
Solutions for Ex 106 to Ex 115
1 parent 12a1590 commit 1c9e4e5

20 files changed

+279
-16
lines changed

Ex105/index.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>Ex 104</title>
7-
<h1>Excercise 104</h1>
6+
<title>Ex 105</title>
7+
<h1>Excercise 105</h1>
88
<script src="index.js"></script>
99
</head>
1010
<body>
1111
<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.
12+
Write a JavaScript program to find the number of times to replace a given number with the sum of its digits.<br>
13+
This is until it converts to a single-digit number.
1414
</p>
1515

1616
</body>

Ex106/index.html

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>Ex 104</title>
7-
<h1>Excercise 104</h1>
6+
<title>Ex 106</title>
7+
<h1>Excercise 106</h1>
88
<script src="index.js"></script>
99
</head>
1010
<body>
1111
<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.
12+
Write a JavaScript program to divide an integer by another integer as long as the result is an integer and return the result.
1413
</p>
1514

1615
</body>

Ex107/index.html

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>Ex 104</title>
7-
<h1>Excercise 104</h1>
6+
<title>Ex 107</title>
7+
<h1>Excercise 107</h1>
88
<script src="index.js"></script>
99
</head>
1010
<body>
1111
<p>
1212
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.
13+
<br>This is such that one element in the pair is divisible by the other one.
14+
<br>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.
1515

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)
16+
<br>For example - The output of [1, 3, 2] ->2 - (1,3), (1,2).
17+
<br>The output of [2, 4, 6] -> 2 - (2,4), (2,6)
18+
<br>The output of [2, 4, 16] -> 3 - (2,4), (2,16), (4,16)
1919
</p>
2020

2121
</body>

Ex107/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
const arr_pairs = (arr) => {
44
let result = 0;
5+
let strResult = "";
6+
let str = [];
7+
// Iterate loop to check each element
58
for(let i = 0; i < arr.length; i++){
9+
// Iterate inner loop to check id either element is a multiple of the other
610
for(let j = i + 1; j < arr.length; j++){
711
if(arr[i] % arr[j] === 0 || arr[j] % arr[i] === 0){
812
result++;
13+
str.push(`(${arr[i]}, ${arr[j]})`);
914
}
1015
}
1116
}
12-
return result;
17+
strResult = str.join(", ") + '.';
18+
return result + "-" + strResult; // Return the total count of pairs
1319
};
1420

1521
console.log(arr_pairs([1,2,3]));

Ex108/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 108</title>
7+
<h1>Excercise 108</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to create the dot products of two given 3D vectors.
13+
<br>Note: The dot product is the sum of the products of the corresponding entries of the two sequences of numbers.
14+
</p>
15+
16+
</body>
17+
</html>

Ex108/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// declare function with two given 3D vectors as the parameters
2+
3+
const dotProduct3DVectors = (vector1, vector2) => {
4+
let product = 0;
5+
if(vector1.length !== 3 && vector2.length !== 3){
6+
throw new Error('Both vectors muct be 3-dimentional');
7+
}
8+
9+
// Calculate the product of the vectors
10+
for(let i = 0; i < 3; i++){
11+
product += vector1[i] * vector2[i];
12+
}
13+
14+
return product;
15+
};
16+
17+
console.log(dotProduct3DVectors([1,2,3], [4,5,6]));

Ex109/index.html

+16
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 109</title>
7+
<h1>Excercise 109</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to sort an array of all prime numbers between 1 and a given integer.
13+
</p>
14+
15+
</body>
16+
</html>

Ex109/index.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// declare function with n as the parameters
2+
3+
const sortPrime = (n) => { // declare the sequence of prime numbers you want to include from 1 to n
4+
let prime_num1 = [], // Array to store prime numbers
5+
prime_num2 = []; // Array to track prime number status
6+
7+
// initialize all entries as true from between 1 to n
8+
for(i = 0; i <= n; i++){
9+
prime_num2.push(true);
10+
}
11+
12+
// Check if it is a prime number using Sieve of Eratosthenes algorithmn
13+
for(i = 2; i <= n; i++){
14+
if(prime_num2[i] == true){
15+
prime_num1.push(i); // Push the prime number to array prime_num1
16+
}
17+
// Mark all multiples of the current prime numbers as false
18+
for(let j = 1; i * j <= n; j++){
19+
prime_num2[i * j] = false;
20+
}
21+
22+
}
23+
return prime_num1;
24+
}
25+
26+
console.log(sortPrime(5));

Ex110/index.html

+16
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 110</title>
7+
<h1>Excercise 110</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to find the number of even values in sequence before the first occurrence of a given number.
13+
</p>
14+
15+
</body>
16+
</html>

Ex110/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// declare function with an array and n(given number) as the parameters
2+
3+
const evenNumCount = (arr, n) => {
4+
let count = 0;
5+
for(let i = 0; i <= arr.length; i++){
6+
if(arr[i] % 2 == 0 && arr[i] !== n){
7+
count++;
8+
}
9+
if(arr[i] == n){
10+
return count;
11+
}
12+
}
13+
return -1;
14+
}
15+
console.log(evenNumCount([1,2,3,4,5,6,7,8,9,10],5))

Ex111/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 111</title>
7+
<h1>Excercise 111</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to check a number from three given numbers where two numbers are equal.
13+
<br>Find the third one.
14+
</p>
15+
16+
</body>
17+
</html>

Ex111/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// declare function with an array and n(given number) as the parameters
2+
3+
const find_third_num = (n1,n2,n3) => {
4+
if(n1 !== n2 && n2 !== n3 && n1 !== n3){
5+
return false
6+
}
7+
else if(n1 !== n2 && n1 == n3){
8+
return n2;
9+
}
10+
else if(n1 == n2 && n1 !== n3){
11+
return n3;
12+
}else if(n2 == n3 && n2 !== n1){
13+
return n1;
14+
}
15+
else{
16+
return `ALl NUMBERS ARE THE SAME`;
17+
}
18+
}
19+
console.log(find_third_num(1,2,2));

Ex112/index.html

+16
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 112</title>
7+
<h1>Excercise 112</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to find the number of trailing zeros in the decimal representation of the factorial of a given number.
13+
</p>
14+
15+
</body>
16+
</html>

Ex112/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// declare function with a number n (factorial) as the parameter
2+
3+
const trailing_zeros = (n) => {
4+
let result = 0;
5+
for(let i = 5; i <= n; i += 5){ // Loop to calculate factorial
6+
let num = i; // Store the current number
7+
while (num % 5 == 0) { // Check if the number is divisible by 5
8+
num /= 5;
9+
result++; // Increment the count of trailing zeros
10+
}
11+
}
12+
return result; // Return the total count of trailing zeros in the factorial of n
13+
}
14+
console.log(trailing_zeros(8));

Ex113/index.html

+16
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 113</title>
7+
<h1>Excercise 113</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to calculate the sum of n + n/2 + n/4 + n/8 + .... where n is a positive integer and all divisions are integers.
13+
</p>
14+
15+
</body>
16+
</html>

Ex113/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// declare function with a number num as the parameter
2+
3+
const int_sum = (num) => {
4+
let s_sum = 0; // Initialize the sum variable
5+
while (num > 0) { // Loop until num is greater than 0
6+
s_sum += num; // Add num to the sum
7+
num = Math.floor(num / 2); // Divide num by 2 using Floor division
8+
}
9+
return s_sum; // Return the sum of integers from 1 to num
10+
}
11+
console.log(int_sum(8));

Ex114/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 114</title>
7+
<h1>Excercise 114</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to check whether a given string represents a correct sentence or not.
13+
<br>A string is considered a correct sentence if it starts with a capital letter and ends with a full stop (.)
14+
</p>
15+
16+
</body>
17+
</html>

Ex114/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// declare function with a string as the parameter
2+
3+
const isSentence = (str) => {
4+
let first_char = str[0]; // Get first Character
5+
let last_char = str[str.length - 1]; // Get last character
6+
return /[A-Z]/.test(first_char) && last_char == ".";
7+
}
8+
console.log(isSentence("This tool will help you write better."));

Ex115/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 115</title>
7+
<h1>Excercise 115</h1>
8+
<script src="index.js"></script>
9+
</head>
10+
<body>
11+
<p>
12+
Write a JavaScript program to check whether a matrix is a diagonal matrix or not.
13+
<br>In linear algebra, a diagonal matrix is a matrix in which the entries outside the main diagonal are all zero (the diagonal from the upper left to the lower right)
14+
</p>
15+
16+
</body>
17+
</html>

Ex115/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// declare function with a matrix as the parameter
2+
3+
const is_diagonal_matrix = (user_matrix) => {
4+
// Loop through each row of the matrix
5+
for(let i = 0; i < user_matrix.length; i++){
6+
// Loop through each element of the matrix
7+
for(let j = 0; j < user_matrix.length; i++){
8+
// Check if the element is non-zero when it is not on the diagonal (i !== j)
9+
if(i !== j && user_matrix[i][j] !== 0){
10+
return false; // return false if non-zero value is found
11+
}
12+
}
13+
}
14+
return true; // Return true if all non-diagonal elements are zero
15+
}
16+
console.log(is_diagonal_matrix([1,0,0],[0,2,0],[0,0,3]));

0 commit comments

Comments
 (0)