Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 104d2ad

Browse files
Merge pull request #63 from tsongtheng/tsongtheng
Add 3 Javascript Problem
2 parents a9672bd + e076482 commit 104d2ad

File tree

6 files changed

+285
-0
lines changed

6 files changed

+285
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function isPrime(num) {
2+
if (num <= 1) {
3+
return false;
4+
}
5+
if (num <= 3) {
6+
return true;
7+
}
8+
if (num % 2 === 0 || num % 3 === 0) {
9+
return false;
10+
}
11+
let i = 5;
12+
while (i * i <= num) {
13+
if (num % i === 0 || num % (i + 2) === 0) {
14+
return false;
15+
}
16+
i += 6;
17+
}
18+
return true;
19+
}
20+
21+
function isPalindrome(num) {
22+
const numStr = num.toString();
23+
const reversedStr = numStr.split("").reverse().join("");
24+
return numStr === reversedStr;
25+
}
26+
27+
function largestPrimePalindrome(n) {
28+
for (let i = n; i >= 2; i--) {
29+
if (isPrime(i) && isPalindrome(i)) {
30+
return i;
31+
}
32+
}
33+
return null; // No prime palindrome found in the range
34+
}
35+
36+
// Test cases
37+
console.log(largestPrimePalindrome(100)); // 7
38+
console.log(largestPrimePalindrome(50)); // 7
39+
console.log(largestPrimePalindrome(10)); // 7
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 0015 Largest Prime Palindrome ( L-A )
2+
3+
## Problem
4+
5+
Write a JavaScript function called largestPrimePalindrome that takes an integer **n** as input and returns the largest prime palindrome that is less than or equal to **n**. A prime palindrome is a number that is both prime and reads the same forward and backward.
6+
7+
**Example**
8+
9+
```
10+
If the input n is 100, the function should return 7 because the largest prime palindrome less than or equal to 100 is 7.
11+
12+
If the input n is 50, the function should return 7 because 7 is the largest prime palindrome less than or equal to 50.
13+
14+
If the input n is 10, the function should return 7 because 7 is the largest prime palindrome less than or equal to 10.
15+
```
16+
17+
## Solutions
18+
19+
```javascript
20+
function isPrime(num) {
21+
if (num <= 1) {
22+
return false;
23+
}
24+
if (num <= 3) {
25+
return true;
26+
}
27+
if (num % 2 === 0 || num % 3 === 0) {
28+
return false;
29+
}
30+
let i = 5;
31+
while (i * i <= num) {
32+
if (num % i === 0 || num % (i + 2) === 0) {
33+
return false;
34+
}
35+
i += 6;
36+
}
37+
return true;
38+
}
39+
40+
function isPalindrome(num) {
41+
const numStr = num.toString();
42+
const reversedStr = numStr.split("").reverse().join("");
43+
return numStr === reversedStr;
44+
}
45+
46+
function largestPrimePalindrome(n) {
47+
for (let i = n; i >= 2; i--) {
48+
if (isPrime(i) && isPalindrome(i)) {
49+
return i;
50+
}
51+
}
52+
return null; // No prime palindrome found in the range
53+
}
54+
55+
// Test cases
56+
console.log(largestPrimePalindrome(100)); // 7
57+
console.log(largestPrimePalindrome(50)); // 7
58+
console.log(largestPrimePalindrome(10)); // 7
59+
```
60+
61+
## How it works
62+
63+
1. The isPrime function checks whether a given number is prime using a primality test that efficiently eliminates multiples of 2 and 3 and then checks for divisibility by numbers of the form 6k ± 1.
64+
65+
2. The isPalindrome function checks whether a given number is a palindrome by converting it to a string, reversing the string, and comparing it to the original string.
66+
67+
3. The largestPrimePalindrome function starts from the given number n and counts down to 2, checking each number in reverse order. It uses the isPrime and isPalindrome functions to find the largest prime palindrome in the given range.
68+
69+
## References
70+
71+
- [ChatGPT](https://chat.openai.com/)
72+
73+
## Problem Added By
74+
75+
- [Tipchan](https://github.com/tsongtheng)
76+
77+
## Contributing
78+
79+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
80+
81+
Please make sure to update tests as appropriate.

‎L-B/0026 Calculate Tip/CalculateTip.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function calculateTip(totalCost, tipPercentage) {
2+
// Calculate the tip amount by multiplying totalCost by tipPercentage divided by 100
3+
const tipAmount = (totalCost * tipPercentage) / 100;
4+
return tipAmount;
5+
}
6+
7+
// Test cases
8+
console.log(calculateTip(50, 15)); // 7.5
9+
console.log(calculateTip(75, 20)); // 15

‎L-B/0026 Calculate Tip/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 0026 Calculate Tip ( L-B )
2+
3+
## Problem
4+
5+
You are at a restaurant, and you want to calculate the tip for your meal. Write a JavaScript function called **calculateTip** that takes two inputs: the total cost of the meal and the tip percentage.Your function should calculate the tip amount and return it.
6+
7+
**Example**
8+
9+
```
10+
If the total cost is 50ドル and the tip percentage is 15%, the function should return 7ドル.5 because 15% of 50ドル is 7ドル.5.
11+
12+
If the total cost is 75ドル and the tip percentage is 20%, the function should return 15ドル because 20% of 75ドル is 15ドル.
13+
```
14+
15+
- **_You can assume that the tip percentage is provided as a whole number (e.g., 15 for 15%) and that the total cost is a positive number._**
16+
17+
**Here's an example of how the function should work:**
18+
19+
```javascript
20+
console.log(calculateTip(50, 15)); // 7.5
21+
console.log(calculateTip(75, 20)); // 15
22+
```
23+
24+
## Solutions
25+
26+
```javascript
27+
function calculateTip(totalCost, tipPercentage) {
28+
// Calculate the tip amount by multiplying totalCost by tipPercentage divided by 100
29+
const tipAmount = (totalCost * tipPercentage) / 100;
30+
return tipAmount;
31+
}
32+
33+
// Test cases
34+
console.log(calculateTip(50, 15)); // 7.5
35+
console.log(calculateTip(75, 20)); // 15
36+
```
37+
38+
## How it works
39+
40+
1. We define a function calculateTip that takes two parameters: totalCost (the total cost of the meal) and tipPercentage (the percentage of the tip to be calculated).
41+
42+
2. Inside the function, we calculate the tip amount by multiplying totalCost by tipPercentage divided by 100. This is because tip percentages are usually provided as whole numbers (e.g., 15 for 15%).
43+
44+
## References
45+
46+
- [ChatGPT](https://chat.openai.com/)
47+
48+
## Problem Added By
49+
50+
- [Tipchan](https://github.com/tsongtheng)
51+
52+
## Contributing
53+
54+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
55+
56+
Please make sure to update tests as appropriate.

‎L-I/0011 DNA_Matching/DNA_Matching.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function dnaMatch(dna1, dna2) {
2+
// Check if the lengths of the two DNA sequences are different
3+
if (dna1.length !== dna2.length) {
4+
return false; // Different lengths, not a match
5+
}
6+
7+
// Iterate through each character of the DNA sequences
8+
for (let i = 0; i < dna1.length; i++) {
9+
// Compare characters at the current position
10+
if (dna1[i] !== dna2[i]) {
11+
return false; // Characters don't match, not a match
12+
}
13+
}
14+
15+
// If we reach this point, all characters match, so it's a match
16+
return true;
17+
}
18+
19+
// Test cases
20+
console.log(dnaMatch("ACGT", "ACGT")); // true
21+
console.log(dnaMatch("ACGT", "ACTG")); // false
22+
console.log(dnaMatch("AACT", "AACC")); // false

‎L-I/0011 DNA_Matching/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 0011 DNA_Matching ( L-I )
2+
3+
## Problem
4+
5+
You are given two DNA sequences as strings, representing the genetic code of two individuals. Each DNA sequence consists of a series of characters, where each character can be one of four options: 'A' (adenine), 'C' (cytosine), 'G' (guanine), or 'T' (thymine).
6+
7+
Write a JavaScript function called dnaMatch that takes these two DNA sequences as input and returns true if they are a match and false otherwise. Two DNA sequences are considered a match if they have the same characters in the same order.
8+
9+
**Example**
10+
11+
```
12+
If the input DNA sequences are "ACGT" and "ACGT", the function should return true because they are an exact match.
13+
If the input DNA sequences are "ACGT" and "ACTG", the function should return false because the order of characters is different.
14+
If the input DNA sequences are "AACT" and "AACC", the function should return true because some characters do not match.
15+
```
16+
17+
**Here's an example of how the function should work:**
18+
19+
```javascript
20+
console.log(dnaMatch("ACGT", "ACGT")); // true
21+
console.log(dnaMatch("ACGT", "ACTG")); // false
22+
console.log(dnaMatch("AACT", "AACC")); // false
23+
```
24+
25+
## Solutions
26+
27+
```javascript
28+
function dnaMatch(dna1, dna2) {
29+
// Check if the lengths of the two DNA sequences are different
30+
if (dna1.length !== dna2.length) {
31+
return false; // Different lengths, not a match
32+
}
33+
34+
// Iterate through each character of the DNA sequences
35+
for (let i = 0; i < dna1.length; i++) {
36+
// Compare characters at the current position
37+
if (dna1[i] !== dna2[i]) {
38+
return false; // Characters don't match, not a match
39+
}
40+
}
41+
42+
// If we reach this point, all characters match, so it's a match
43+
return true;
44+
}
45+
46+
// Test cases
47+
console.log(dnaMatch("ACGT", "ACGT")); // true
48+
console.log(dnaMatch("ACGT", "ACTG")); // false
49+
console.log(dnaMatch("AACT", "AACC")); // false
50+
```
51+
52+
## How it works
53+
54+
1. The `dnaMatch` function takes two input arguments: `dna1` and `dna2`, which are the two DNA sequences we want to compare.
55+
56+
2. First, it checks if the lengths of `dna1` and `dna2` are different using the `if (dna1.length !== dna2.length)` conditional statement. If they have different lengths, the function immediately returns `false` because sequences of different lengths cannot be a match.
57+
58+
3. If the lengths are the same, the function proceeds to compare the characters of the two DNA sequences.
59+
60+
4. It enters a `for` loop that iterates through the characters of the sequences. The loop is controlled by the variable `i`, which starts at 0 and goes up to `dna1.length - 1`. This loop allows us to compare characters at the same position in both sequences.
61+
62+
5. Inside the loop, the code compares the characters at the current position `i` using the conditional statement `if (dna1[i] !== dna2[i])`. If the characters at position `i` are not the same in both sequences, the function immediately returns `false` because it found a mismatch.
63+
64+
6. If the loop completes without finding any mismatches, meaning it has compared all characters in both sequences, the function returns `true`. This indicates that the two DNA sequences are identical and, therefore, a match.
65+
66+
## References
67+
68+
- [ChatGPT](https://chat.openai.com/)
69+
70+
## Problem Added By
71+
72+
- [Tipchan](https://github.com/tsongtheng)
73+
74+
## Contributing
75+
76+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
77+
78+
Please make sure to update tests as appropriate.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /