Skip to content

Commit e31d68d

Browse files
committed
71
1 parent 83f9d55 commit e31d68d

File tree

2 files changed

+28
-11
lines changed
  • 05-javascript-a-ds-new/2-advanced-javascript/2d-learn-recursion-by-building-binary-converter

2 files changed

+28
-11
lines changed

05-javascript-a-ds-new/2-advanced-javascript/2d-learn-recursion-by-building-binary-converter/README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,29 @@
396396
</details>
397397

398398
* I clear out the `decimalToBinary()` function and write the base case of the recursive call:
399-
```js
400-
const decimalToBinary = (input) => {
401-
if (input === 0) {
402-
return "";
403-
}
404-
};
405-
```
399+
```js
400+
const decimalToBinary = (input) => {
401+
if (input === 0) {
402+
return "";
403+
}
404+
};
405+
```
406406
* I need to halve my input, and append it to the remainder of `input ÷ 2`, so I add the following recursion case:
407-
```js
407+
```js
408+
const decimalToBinary = (input) => {
409+
if (input === 0) {
410+
return "";
411+
} else {
412+
return decimalToBinary(Math.floor(input / 2)) + (input % 2);
413+
}
414+
}
415+
```
416+
* I still need to set the text of the `result` div, I do this in the `checkUserInput()` function:
417+
```js
418+
const checkUserInput = () => {
419+
// EXISTING CODE
408420

409-
```
421+
result.textContent = decimalToBinary(parseInt(numberInput.value))
422+
numberInput.value = ''
423+
}
424+
```

05-javascript-a-ds-new/2-advanced-javascript/2d-learn-recursion-by-building-binary-converter/code/script.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ const result = document.getElementById("result")
55
const decimalToBinary = (input) => {
66
if (input === 0) {
77
return "";
8+
} else {
9+
return decimalToBinary(Math.floor(input / 2)) + (input % 2);
810
}
9-
};
11+
}
1012

1113
const checkUserInput = () => {
1214
if (!numberInput.value || isNaN(parseInt(numberInput.value)) || parseInt(numberInput.value)<0) {
1315
alert("Please provide a decimal number greater than or equal to 0");
1416
return;
1517
}
1618

17-
decimalToBinary(parseInt(numberInput.value))
19+
result.textContent = decimalToBinary(parseInt(numberInput.value))
1820
numberInput.value = ''
1921
}
2022

0 commit comments

Comments
 (0)