Skip to content
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

Feature Enhancement: Added Decimal Output, Division Method, OR, and XOR Operators for Binary Inputs #1853

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions Calculators/BMI-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,20 @@ <h1>BMI Calculator</h1>
</div>
<div class="containerHW">
<div class="inputH">
<label for="heightUnit">Height Unit</label>
<select id="heightUnit" onchange="toggleHeightInput()">
<option value="cm">cm</option>
<option value="feet">feet</option>
</select>
</div>
<div class="inputH" id="heightCmInput">
<label for="height">Height(cm)</label>
<input type="number" id="height" required>
</div>
<div class="inputH" id="heightFeetInput" style="display: none;">
<label for="heightFeet">Height(feet)</label>
<input type="number" id="heightFeet" required>
</div>
<div class="inputW">
<label for="weight">Weight(kg)</label>
<input type="number" id="weight" required>
Expand Down
36 changes: 32 additions & 4 deletions Calculators/BMI-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,50 @@ var span = document.getElementsByClassName("close")[0];
// document.querySelector("#result").innerHTML = "00.00";

function calculate() {

var heightUnit = document.getElementById("heightUnit").value;
var heightValue;

if (heightUnit === "cm") {
heightValue = document.getElementById("height").value;
} else {
var heightFeet = document.getElementById("heightFeet").value;
heightValue = convertFeetToCm(heightFeet);
}

if (age.value == '' || height.value == '' || weight.value == '' || (male.checked == false && female.checked == false)) {
modal.style.display = "block";
modalText.innerHTML = 'ALL fields are required!';
} else if (!isPositiveNumber(height.value) || !isPositiveNumber(weight.value)) {
modal.style.display = "block";
modalText.innerHTML = 'Please enter valid positive values for height and weight!';
} else {
countBmi();
countBmi(heightValue);
}
}
function isPositiveNumber(value) {
return /^\d*\.?\d+$/.test(value) && parseFloat(value) > 0;
}
function countBmi() {
var p = [age.value, height.value, weight.value];
function convertFeetToCm(feet) {
return (feet * 30.48) ;
}
function toggleHeightInput() {
var heightUnit = document.getElementById("heightUnit").value;
var heightLabel = document.querySelector("label[for='height']");

if (heightUnit === "cm") {
document.getElementById("heightCmInput").style.display = "block";
document.getElementById("heightFeetInput").style.display = "none";
heightLabel.textContent = "Height(cm)";
} else {
document.getElementById("heightCmInput").style.display = "none";
document.getElementById("heightFeetInput").style.display = "block";
heightLabel.textContent = "Height(feet)";

}
}
function countBmi(heightValue) {
var p = [age.value, heightValue, weight.value];
if (male.checked) {
p.push("male");
} else if (female.checked) {
Expand Down Expand Up @@ -60,7 +89,6 @@ function countBmi() {

resultArea.style.display = "block";
document.querySelector(".comment").innerHTML = `You are <span id="comment">${result}</span>`;
// Update the result only after the calculation
document.querySelector("#result").innerHTML = bmi.toFixed(2);
}

Expand Down
4 changes: 3 additions & 1 deletion Calculators/BMI-Calculator/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,6 @@ button.calculate:hover {
.linkDownload {
display: none;
}
}
}


8 changes: 8 additions & 0 deletions Calculators/Binary-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ <h1>Binary Calculator</h1>
<option value="add">Addition</option>
<option value="subtract">Subtraction</option>
<option value="multiply">Multiplication</option>
<option value="division">Division</option>
<option value="leftShift">Left Shift</option>
<option value="rightShift">Right Shift</option>
<option value="and">AND Operator</option>
<option value="or">OR Operator</option>
<option value="xor">XOR Operator</option>
</select>
</div>
<button type="button" onclick="calculate()">Calculate</button>
</form>
<div id="results">
<p id="result"></p>
<p id="decimal"></p>
</div>
<div id="decimals">

</div>
</div>
<script src="script.js"></script>
Expand Down
24 changes: 23 additions & 1 deletion Calculators/Binary-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,44 @@ function calculate() {
const num1 = parseInt(binary1, 2);
const num2 = parseInt(binary2, 2);
let result;

let decimal;
// Perform the selected operation
switch (operation) {
case "add":
result = (num1 + num2).toString(2);
decimal = (num1 + num2);
break;
case "subtract":
result = (num1 - num2).toString(2);
decimal = (num1 - num2);
break;
case "multiply":
result = (num1 * num2).toString(2);
decimal = (num1 * num2);
break;
case "leftShift":
result = (num1 << num2).toString(2);
decimal = (num1 << num2);
break;
case "rightShift":
result = (num1 >> num2).toString(2);
decimal = (num1 >> num2);
break;
case "division":
result = (num1 / num2).toString(2);
decimal = (num1 / num2);
break;
case "and":
result = (num1 && num2).toString(2);
decimal = (num1 && num2);
break;
case "or":
result = (num1 || num2).toString(2);
decimal = (num1 || num2);
break;
case "xor":
result = (num1 ^ num2).toString(2);
decimal = (num1 ^ num2);
break;
default:
alert("Invalid operation.");
Expand All @@ -39,4 +60,5 @@ function calculate() {

// Display the result
document.getElementById('result').innerText = `Result: ${result}`;
document.getElementById('decimal').innerText = `Decimal Value: ${decimal}`;
}
3 changes: 2 additions & 1 deletion Calculators/Binary-Calculator/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ body {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
transition: transform 0.3s;
width: 500px;
height: 400px;
height: 450px;
}

.container:hover {
Expand Down Expand Up @@ -79,6 +79,7 @@ button:hover {
}

#results {

margin-top: 20px;
text-align: center;
}