Skip to content

Commit 35d5f7b

Browse files
authored
Merge pull request #19 from sahilatahar/main
Feature: Add First Challenge - Sum of N Natural Numbers, and Second Challenge - Decimal to Binary Conversion
2 parents ff89086 + 5393a69 commit 35d5f7b

File tree

7 files changed

+154
-0
lines changed

7 files changed

+154
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ Write a function called `celsiusToFahrenheit` that takes a temperature in Celsiu
154154

155155
[Solution Explanation](./solutions/ch_16_Celsius_To_Fahrenheit/readme.md)
156156

157+
## Challenge 17: Calculate the Sum of N Natural Number.
158+
159+
Write a function that takes a number as input and returns sum from 1 to `n`. For example, if the input is 10, the function should return 55.
160+
161+
Write a function called `getNaturalSum` that takes a number `n` as its parameter and returns sum of natural number.
162+
163+
[Solution Explanation](./solutions/ch_17_Sum_Of_N_Natural_Number/readme.md)
164+
165+
## Challenge 18: Convert Decimal to Binary.
166+
167+
Write a function that takes a decimal number as input and returns binary string. For example, if the input is 12, the function should return 1100.
168+
169+
Write a function called `decimalToBinary` that takes a decimal number as `decimal` in its parameter and returns binary string.
170+
171+
[Solution Explanation](./solutions/ch_18_Decimal_To_Binary/readme.md)
172+
157173
<!-- Add new challenges before this comment -->
158174

159175
## Contributors
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Function that calculates the sum of n natural number.
2+
function getNaturalSum(n) {
3+
let sum = 0; // Declare a sum variable to store the sum
4+
5+
// Running loop from 1 to n
6+
for (let i = 1; i <= n; i++) {
7+
// Adding the sum by i
8+
sum += i; // sum = sum + i;
9+
}
10+
11+
return sum; // Return the sum
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Function that calculates the sum of n natural number.
2+
function getNaturalSum(n) {
3+
// Using ternary operator (?:)
4+
return n <= 1 ? n : n + getNaturalSum(n - 1);
5+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Challenge 17: Calculate the Factorial of a Number.
2+
3+
Write a function that takes a number as input and returns sum from 1 to `n`. For example, if the input is 10, the function should return 55.
4+
5+
Write a function called `getNaturalSum` that takes a number `n` as its parameter and returns sum of natural number.
6+
7+
## Answer 1
8+
9+
```javascript
10+
// Function that calculates the sum of n natural number.
11+
function getNaturalSum(n) {
12+
let sum = 0; // Declare a sum variable to store the sum
13+
14+
// Running loop from 1 to n
15+
for (let i = 1; i <= n; i++) {
16+
// Adding the sum by i
17+
sum += i; // sum = sum + i;
18+
}
19+
20+
return sum; // Return the sum
21+
}
22+
```
23+
24+
## Answer 1 Explanation
25+
26+
In the `getNaturalSum` function, declare `sum` variable to store the sum and initializing with 0 and then running a loop from 1 to `n` and adding the `sum` with all the numbers from 1 to `num`. After finishing the loop return the `sum`.
27+
28+
## Answer 2 using Recursion
29+
30+
```javascript
31+
// Function that calculates the sum of n natural number.
32+
function getNaturalSum(n) {
33+
// Using ternary operator (?:)
34+
return n <= 1 ? n : n + getNaturalSum(n - 1);
35+
}
36+
```
37+
38+
## Answer 2 Explanation
39+
40+
In the `getNaturalSum` function, checking value of `n` if `n` is more than 1 then adding `n` and return value of `getNaturalSum` with less than 1 `n` value. if value is less than or equal to 1 then it will return n, that value, and recursive function will return sum.
41+
42+
Here is example how recursive function calculate factorial of 5:
43+
44+
```JavaScript
45+
function getFactorial(num) {
46+
return 5 + getFactorial(4);
47+
4 + getFactorial(3);
48+
3 + getFactorial(2);
49+
2 + getFactorial(1);
50+
return 1;
51+
}
52+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Function to convert decimal numbers to binary strings
2+
function decimalToBinary(decimal) {
3+
let binaryString = ""; // Initialize an empty string to store the binary digits
4+
5+
// Perform repeated division by 2 until the decimal number becomes 0
6+
while (decimal > 0) {
7+
// Append the remainder of division (0 or 1) to the binary string
8+
binaryString = (decimal % 2) + binaryString;
9+
10+
// Update the decimal number by integer division (floor division) by 2
11+
decimal = Math.floor(decimal / 2);
12+
}
13+
14+
// Return the binary string
15+
return binaryString;
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function decimalToBinary1(decimal) {
2+
// Convert the decimal number to a binary string using the toString() method
3+
let binaryString = decimal.toString(2);
4+
5+
// Return the binary string
6+
return binaryString;
7+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Challenge 18: Convert Decimal to Binary.
2+
3+
Write a function that takes a decimal number as input and returns binary string. For example, if the input is 12, the function should return 1100.
4+
5+
Write a function called `decimalToBinary` that takes a decimal number as `decimal` in its parameter and returns binary string.
6+
7+
## Answer 1
8+
9+
```javascript
10+
// Function to convert decimal numbers to binary strings
11+
function decimalToBinary(decimal) {
12+
let binaryString = ""; // Initialize an empty string to store the binary digits
13+
14+
// Perform repeated division by 2 until the decimal number becomes 0
15+
while (decimal > 0) {
16+
// Append the remainder of division (0 or 1) to the binary string
17+
binaryString = (decimal % 2) + binaryString;
18+
19+
// Update the decimal number by integer division (floor division) by 2
20+
decimal = Math.floor(decimal / 2);
21+
}
22+
23+
// Return the binary string
24+
return binaryString;
25+
}
26+
```
27+
28+
## Answer 1 Explanation
29+
30+
In the `decimalToBinary` function, declare `binaryString` variable to store the binary string with empty string and then running a while loop until decimal is greater than one. Inside loop adding remainder in `binaryString` and dividing `decimal` itself by 2. After finishing the loop return `binaryString`.
31+
32+
## Answer 2 using toString()
33+
34+
```javascript
35+
function decimalToBinary(decimal) {
36+
// Convert the decimal number to a binary string using the toString() method
37+
let binaryString = decimal.toString(2);
38+
39+
// Return the binary string
40+
return binaryString;
41+
}
42+
```
43+
44+
## Answer 2 Explanation
45+
46+
`decimalToBinary` the built-in `toString()` method with a base argument of 2. This method directly converts the decimal number to a binary string.

0 commit comments

Comments
 (0)