Skip to content

Feature: Add First Challenge - Sum of N Natural Numbers, and Second Challenge - Decimal to Binary Conversion #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ Write a function called `celsiusToFahrenheit` that takes a temperature in Celsiu

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

## Challenge 17: Calculate the Sum of N Natural Number.

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.

Write a function called `getNaturalSum` that takes a number `n` as its parameter and returns sum of natural number.

[Solution Explanation](./solutions/ch_17_Sum_Of_N_Natural_Number/readme.md)

## Challenge 18: Convert Decimal to Binary.

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.

Write a function called `decimalToBinary` that takes a decimal number as `decimal` in its parameter and returns binary string.

[Solution Explanation](./solutions/ch_18_Decimal_To_Binary/readme.md)

<!-- Add new challenges before this comment -->

## Contributors
Expand Down
12 changes: 12 additions & 0 deletions solutions/ch_17_Sum_Of_N_Natural_Number/answer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Function that calculates the sum of n natural number.
function getNaturalSum(n) {
let sum = 0; // Declare a sum variable to store the sum

// Running loop from 1 to n
for (let i = 1; i <= n; i++) {
// Adding the sum by i
sum += i; // sum = sum + i;
}

return sum; // Return the sum
}
5 changes: 5 additions & 0 deletions solutions/ch_17_Sum_Of_N_Natural_Number/answer2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Function that calculates the sum of n natural number.
function getNaturalSum(n) {
// Using ternary operator (?:)
return n <= 1 ? n : n + getNaturalSum(n - 1);
}
52 changes: 52 additions & 0 deletions solutions/ch_17_Sum_Of_N_Natural_Number/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Challenge 17: Calculate the Factorial of a Number.

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.

Write a function called `getNaturalSum` that takes a number `n` as its parameter and returns sum of natural number.

## Answer 1

```javascript
// Function that calculates the sum of n natural number.
function getNaturalSum(n) {
let sum = 0; // Declare a sum variable to store the sum

// Running loop from 1 to n
for (let i = 1; i <= n; i++) {
// Adding the sum by i
sum += i; // sum = sum + i;
}

return sum; // Return the sum
}
```

## Answer 1 Explanation

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`.

## Answer 2 using Recursion

```javascript
// Function that calculates the sum of n natural number.
function getNaturalSum(n) {
// Using ternary operator (?:)
return n <= 1 ? n : n + getNaturalSum(n - 1);
}
```

## Answer 2 Explanation

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.

Here is example how recursive function calculate factorial of 5:

```JavaScript
function getFactorial(num) {
return 5 + getFactorial(4);
4 + getFactorial(3);
3 + getFactorial(2);
2 + getFactorial(1);
return 1;
}
```
16 changes: 16 additions & 0 deletions solutions/ch_18_Decimal_To_Binary/answer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Function to convert decimal numbers to binary strings
function decimalToBinary(decimal) {
let binaryString = ""; // Initialize an empty string to store the binary digits

// Perform repeated division by 2 until the decimal number becomes 0
while (decimal > 0) {
// Append the remainder of division (0 or 1) to the binary string
binaryString = (decimal % 2) + binaryString;

// Update the decimal number by integer division (floor division) by 2
decimal = Math.floor(decimal / 2);
}

// Return the binary string
return binaryString;
}
7 changes: 7 additions & 0 deletions solutions/ch_18_Decimal_To_Binary/answer2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function decimalToBinary1(decimal) {
// Convert the decimal number to a binary string using the toString() method
let binaryString = decimal.toString(2);

// Return the binary string
return binaryString;
}
46 changes: 46 additions & 0 deletions solutions/ch_18_Decimal_To_Binary/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Challenge 18: Convert Decimal to Binary.

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.

Write a function called `decimalToBinary` that takes a decimal number as `decimal` in its parameter and returns binary string.

## Answer 1

```javascript
// Function to convert decimal numbers to binary strings
function decimalToBinary(decimal) {
let binaryString = ""; // Initialize an empty string to store the binary digits

// Perform repeated division by 2 until the decimal number becomes 0
while (decimal > 0) {
// Append the remainder of division (0 or 1) to the binary string
binaryString = (decimal % 2) + binaryString;

// Update the decimal number by integer division (floor division) by 2
decimal = Math.floor(decimal / 2);
}

// Return the binary string
return binaryString;
}
```

## Answer 1 Explanation

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`.

## Answer 2 using toString()

```javascript
function decimalToBinary(decimal) {
// Convert the decimal number to a binary string using the toString() method
let binaryString = decimal.toString(2);

// Return the binary string
return binaryString;
}
```

## Answer 2 Explanation

`decimalToBinary` the built-in `toString()` method with a base argument of 2. This method directly converts the decimal number to a binary string.