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

Fixed negative input in Logarithm Calculator #660

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Calculators/Logarithm-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ <h1>Logarithm Calculator</h1>
<hr>
<div class="content">
<p>Calculate Logarithm of a number to any base:</p>
<div id="base1"><input type="number" id="base" placeholder="Enter base"></div>
<div id="number1"><input type="number" id="number" placeholder="Enter number"></div>
<div id="base1"><input type="number" id="base" min="0" onchange="validateInput()" oninput="validity.valid||(value='');" step="any" placeholder="Enter base"></div>
<div id="number1"><input type="number" id="number" min="1" oninput="validity.valid||(value='');" placeholder="Enter number"></div>
<button onclick="calculateLog()" id="button">Calculate Log</button>
<h3>Result:</h3>
<p id="result"></p>
Expand Down
7 changes: 7 additions & 0 deletions Calculators/Logarithm-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ function calculateLog() {
const number = parseFloat(document.getElementById('number').value);
const base = parseFloat(document.getElementById('base').value);


if (!isNaN(number) && !isNaN(base) && number > 0 && base > 0 && base !== 1) {
const result = Math.log(number) / Math.log(base);
document.getElementById('result').innerText = `Log base ${base} of ${number} is ${result.toFixed(4)}`;
} else {
alert('Invalid input. Please enter positive numbers for both the base and the number, and ensure that the base is not equal to 1.')
}
}
function validateInput(){
var input=document.getElementById('base');
if(input.value==1 || input.value==0){
alert("Invalid input log's base can take value between 0 & 1 or positive value except 1");
}
}
Loading