'Why does function showPI() return empty string?
I don't know why function showPI() returns empty string. I triggered console.log(trimmedPI) in this function before return statement and it showed string. But when I put showPI() function in compare() function it didn't work. String is empty and my app doesn't work. Maybe I am tired, because there is 00:30 AM in my country. I can't see mistake, there is no errors in console.
function getInput() {
const textInput = document.getElementById("textInput").value;
const PiWithout_3 = textInput.substring(2, textInput.length);
let PiDigitsWithSpace = PiWithout_3.match(/.{1,5}/g);
PiDigitsWithSpace = PiDigitsWithSpace.join(' ');
PiDigitsWithSpace = "3, " + PiDigitsWithSpace;
document.getElementById("pFromInput").textContent = PiDigitsWithSpace;
const textInputLength = textInput.length - 2;
document.getElementById("numbersCount").innerHTML = "You entered <b>" + textInputLength + "</b> Pi digits after comma.";
return textInput;
}
let textInput = document.getElementById("textInput");
function showPI() {
const PI = "3,1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989";
const trimmed = document.getElementById("trimmed").value;
const trimmedInt = parseInt(trimmed) + 2;
const trimmedPI = PI.substring(0, trimmedInt);
document.getElementById("pShowPi").innerHTML = trimmedPI;
return trimmedPI;
}
textInput.oninput = function compare() {
const textInput = document.getElementById("textInput").value;
const compareResult = document.getElementById("compareResult");
if (showPI() === getInput()) {
compareResult.innerHTML = " <b>correct</b>";
} else {
compareResult.innerHTML = " <b>incorrect</b>";
}
}
const button = document.querySelector("#showPi");
button.addEventListener('click', showPI);
* {
margin: 0;
padding: 0;
}
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
margin-top: 50px;
}
h1 {
font-size: 24px;
}
label {
display: block;
padding-bottom: 5px;
padding-top: 10px;
}
p {
padding: 10px 0;
word-wrap: break-word;
}
#wrapper {
width: 493px;
padding: 10px;
background: rgb(255 246 230);
}
input {
height: 26px;
border: 1px solid #de8f02;
border-radius: 3px;
padding: 5px;
}
input:focus,
select:focus,
textarea:focus,
file:focus {
outline: none;
}
input:hover,
input:focus {
border: 2px solid #b97602 !important;
}
input::placeholder {
color: rgb(175, 175, 175);
opacity: 1;
}
#textInput {
width: 380px;
}
#pFromInput {
font-size: 24px;
}
#compareResult {
font-size: 20px;
}
#numbersCount {
border-bottom: 1px solid #ffdda2;
}
#showPi {
background-color: #de8f02;
border: none;
color: #fff6e6;
padding: 5px 5px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
border-radius: 4px;
cursor: pointer;
}
#showPi:hover {
background-color: #b97602;
transition: 300ms;
}
<div id="wrapper">
<h1>Memorizing π</h1>
<label for="textInput">π from your memory (with comma)</label>
<input type="text" id="textInput" placeholder="3,">
<p id="pFromInput"></p>
<p id="numbersCount"></p>
<label for="trimmed">1÷1000 π digits after comma</label>
<input type="number" id="trimmed" min="1" max="1000">
<button id="showPi">show Pi</button>
<p id="pShowPi"></p>
<p id="compareResult"></p>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
