Skip to content

Update index.js #2

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 39 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ function App() {
const [binaryText, setBinaryText] = useState('')
const [decimalText, setDecimalText] = useState('')
const [errorMessage, setErrorMessage] = useState('')

// Function to calculate numbers with decimal places
const convertWithFraction = binarytext => {
let number = binarytext.toString().split('.')
let numberFractionResult = 0
let result
for (let i = 0; i < number[1].length ; ++i) {
let j = (i+1)*-1
numberFractionResult += number[i] * Math.pow(2, j)
}
result = Number(parseInt(number[0]) + numberFractionResult)
return result
}

// Perform the conversion on form submit
const onFormSubmit = e => {
Expand All @@ -26,22 +39,34 @@ function App() {
}

setErrorMessage('') // Reset the error message

// Condition if the user wants to convert numbers with decimal places
if ( binaryText.split('.').length === 2 && binaryText.split('.')[1] ==! null ) {
const result = convertWithFraction( binaryText );
setErrorMessage('Enter either 0 or 1')
// I used to prevent the user from clicking the dot and not (.) the decimal part, and also prevents several (.)
setDecimalText(result === 'NaN' ? result : '')
// Condition if the user wants to convert integer number
} else {

// Formulae:
// input = 1 => output = 1 * (2^0) = 1
// input = 10 => output = (0 * (2^0)) + (1 * (2^1)) = 2
// So we reverse and iterate from the back
const reversedBinaryText = binaryText
.split('')
.map(Number) // Convert to a number from string
.reverse()
// Formulae:
// input = 1 => output = 1 * (2^0) = 1
// input = 10 => output = (0 * (2^0)) + (1 * (2^1)) = 2
// So we reverse and iterate from the back
const reversedBinaryText = binaryText
.split('')
.map(Number) // Convert to a number from string
.reverse()

// Calculate the result by accumulating previous vaue
const result = reversedBinaryText.reduce(
(accumulator, currentValue, idx) =>
accumulator + currentValue * Math.pow(2, idx)
)
setDecimalText(result)
// Calculate the result by accumulating previous vaue
const result = reversedBinaryText.reduce(
(accumulator, currentValue, idx) =>
accumulator + currentValue * Math.pow(2, idx)
)
setErrorMessage('Enter either 0 or 1')
// I used to prevent the user from clicking the dot and not (.) the decimal part, and also prevents several (.)
setDecimalText(result === 'NaN' ? result : '')
}
}

return (
Expand Down