123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed&display=swap" rel="stylesheet">
<body>
  <h2>Each Day: &nbsp;How Much Water Should You Drink?</h2>
  <br>
  <button id="addPersonBtn">Add Another Person</button>
  
  <div id="people">
  </div>
  
  <div class="refInfo">
    <hr>
    Calculations are based on article on Goodhousekeeping web site found:
    <a target="_blank"
       href="https://www.goodhousekeeping.com/health/diet-nutrition/a46956/how-much-water-should-i-drink/">here</a>
    
    <p><small>there could be some rounding errors...</small></p>
  </div>
</body>
            
          
!
            
              body {
  background: #e6eeff;
}

h2 {
  color:#0066ff;
  padding-bottom:0px;
  margin-bottom:2px;
  font-family: 'Roboto Condensed', sans-serif;
}

button {
  cursor:pointer;
}

input {
  border: solid silver .5px;
  border-radius:3px;
}

.person {
  position:absolute;
  top:105px;
  border-right: solid lightblue .5px;
  width:320px;
  min-height:220px;
}

.personNameLbl {
  position:absolute;
  left:0px;
  top:0px;
  width:140px;
  background:#fff7e6;
  font-family: 'Roboto Condensed', sans-serif;
}

.personNameInput {
  position:absolute;
  left:140px;
  top:0px;
  width:170px;
  background:#ffcc66;
}

.ageLbl {
  position:absolute;
  left:0px;
  top:25px;  
  font-family: 'Roboto Condensed', sans-serif;
}

.ageInput {
  position:absolute;
  left:140px;
  top:25px;
  width:40px;
  text-align:right;
}

.weightLbl {
  position:absolute;
  left:0px;
  top:50px;  
  font-family: 'Roboto Condensed', sans-serif;
}

.weightInput {
  position:absolute;
  left:140px;
  top:50px;
  width:40px;
  text-align:right;
}

.minutesLbl {
  position:absolute;
  left:0px;
  top:75px;  
  width:140px;
  background:lightyellow;
  font-family: 'Roboto Condensed', sans-serif;
}

.minutesInput {
  position:absolute;
  left:140px;
  top:75px;
  width:40px;
  text-align:right;
}

.waterToDrinkLbl {
  position:absolute;
  color:#003d99;
  left:0px;
  top:120px;  
  width:260px;
  font-weight:bold;
  font-size:14pt;
  font-family: 'Roboto Condensed', sans-serif;
}

.ozsLbl {
  position:absolute;
  left:20px;
  top:150px;  
  width:140px;
  font-family: 'Roboto Condensed', sans-serif;
}

.ozInput {
  position:absolute;
  left:140px;
  top:150px;
  width:40px;
  text-align:right;
}

.cupsLbl {
  position:absolute;
  left:20px;
  top:175px;  
  width:140px;
  font-family: 'Roboto Condensed', sans-serif;
}

.cupsInput {
  position:absolute;
  left:140px;
  top:175px;
  width:40px;
  text-align:right;
}




.delBtn {
  position:absolute;
  left:20px;
  top:220px;  
  width:140px;
  background:#800000;
  color:lightyellow;
  border:solid red .5px;
  border-radius:4px;
  height:25px;
}

.refInfo {
  position:absolute;
  left:20px;
  top:390px;
  font-family: 'Roboto Condensed', sans-serif;
}
            
          
!
            
              let people = [];

/********************************************************
  1) Take weight in pounds and divide by 2.2
  2) Multiply that # depending on your age:
     a) Younger than 30... by 30
     b) Between 30 and 55... by 35
     c) Older than 55... by 30
  3) Divide that sum by 28.3 
     (this gives you the total # of ounces you
      should drink each day)
  4) Divide # by 8 to see your result in cups
  
  5) Add an extra .8 oz per minute of exercise
  
  My wife got the info from the goodhousekeeping.com web site
  Sept 8, 2019
  
  https://www.goodhousekeeping.com/health/diet-nutrition/a46956/how-much-water-should-i-drink/
********************************************************/

function pageSetup(event) {
  const addPersonBtnNd = document.getElementById("addPersonBtn");
  addPersonBtnNd.addEventListener('click', addPerson);
  
  if (localStorage.getItem("people")) {
    sData = localStorage.getItem("people");
    people = JSON.parse(sData);
    displayPeople();
  } else {
    addPerson();
  } // end if/else
  
  setTimeout(firstFocus, 20);
} // end of function pageSetup()


function firstFocus() {
  const age1Nd = document.getElementById("age0");
  age1Nd.focus();
} // end of function firstFocus()


//
function addPerson(event) {
  const person = {};
  person.name = "Person "+(people.length+1);
  person.weight = 150;
  person.age = 35;
  person.minutesExercise = 0;
  person.oz = 0;  // will be calced later
  person.cups = 0;   // will be calced later
  people.push(person);
  savePeople();
  displayPeople();
} // end of function addPerson()


function displayPeople() {
  const nMax = people.length;
  const s=[];
  const Q = '"';
  
  for (let n=0;n<nMax;n++) {
    const person = people[n];
    s.push("<div class='person' ")
    s.push("style="+Q);
    let nLeft = (n * 335 + 10);
    s.push("left:"+ (nLeft) +"px;");
    s.push(Q);
    s.push(">")
      s.push("<span class='personNameLbl'>Name:</span>")
      s.push("<input class='personNameInput'id='pname"+n+"'");
      s.push(" data-idx="+Q+n+Q+" ");
      s.push(">")
      s.push("<span class='ageLbl' title='Age in years...'>Age:</span>")
      s.push("<input class='ageInput' id='age"+n+"'")
      s.push(" data-idx="+Q+n+Q+" ");
      s.push(" value='"+person.age+"'");
      s.push(">");
      s.push("<span class='weightLbl' title='Weight in Pounds'>Weight (lbs):</span>")
      s.push("<input class='weightInput' id='lbs"+n+"'");
      s.push(" data-idx="+Q+n+Q+" ");
      s.push(" value='"+person.weight+"'");
      s.push(">");
    
      let s1=[];
      s.push("<span class='minutesLbl' title='Minutes of Exercise per day'>Minutes of Exercise:</span>");
      s.push("<input class='minutesInput' id='mins"+n+"'")
      s.push(" data-idx="+Q+n+Q+" ");
      s.push(" value='"+person.minutesExercise+"'");
      s.push(">");

   // alert(s1.join('\n'));
      s.push("<span class='waterToDrinkLbl'>Amount of Water to Drink:</span>");
      s.push("<span class='ozsLbl'>In Ounces:</span>");
    
      s.push("<input class='ozInput' id='oz"+n+"'");
      s.push(" data-idx="+Q+n+Q+" ");
      s.push(" readonly value='"+person.oz+"'");
      s.push(">");
    
      s.push("<span class='cupsLbl'>In 8oz Cups:</span>");
    
      s.push("<input class='cupsInput' id='cups"+n+"'");
      s.push(" data-idx="+Q+n+Q+" ");
      s.push(" readonly value='"+person.cups+"'");
      s.push(">");
    
      if (nMax>1) {
        s.push("<button class='delBtn' ");
        s.push("id='delBtn"+n+"' ");
        s.push(">Delete Person</button>");
      } // end if
    
    s.push("</div>"); // person div
  } // next n
  
  const peopleNd = document.getElementById("people");
  peopleNd.innerHTML = s.join("");
  
  for (let n=0;n<nMax;n++) {
    const person = people[n];
    const nameNd = document.getElementById("pname"+n);
    nameNd.value = person.name;
    nameNd.addEventListener('keyup', updatePersonName);
    
    const ageNd = document.getElementById("age"+n);
    ageNd.addEventListener('keydown', checkIntInput);
    ageNd.addEventListener('keyup', updatePersonAge);
    
    const lbsNd = document.getElementById("lbs"+n);
    lbsNd.addEventListener('keydown', checkIntInput);
    lbsNd.addEventListener('keyup', updatePersonWeight);
    
    const minsNd = document.getElementById("mins"+n);
    minsNd.addEventListener('keydown', checkIntInput);
    minsNd.addEventListener('keyup', updatePersonMinutes);
    
    if (nMax>1) {
      const delBtnNd = document.getElementById("delBtn"+n);
      delBtnNd.addEventListener('click', delPerson); // click event handler for Delete button
    } // end if
    
    doCalc(n);
  } // next n
  
} // end of function displayPeople()



function doCalc(nIndex) {
  const person = people[nIndex];
  const ageInputNd = document.getElementById("age"+nIndex);
  const nAge = ageInputNd.value - 0;
  const weightInputNd = document.getElementById("lbs"+nIndex);
  const nWeight = weightInputNd.value - 0;
  const minutesInputNd = document.getElementById("mins"+nIndex);
  const nMinutes = minutesInputNd.value - 0; // daily minutes of exercise
  let v1 = nWeight / 2.2;
  
  if (nAge < 30 || nAge > 55) {
    v1 = v1 * 30.0;
  } else {
    v1 = v1 * 35.0;
  } // end if/else
  
  let nBaseOzToDrink = v1 / 28.3;
  let nTotOzToDrink = nBaseOzToDrink + (nMinutes * 0.8);
  nTotOzToDrink = nTotOzToDrink.toPrecision(4);
  let nTotCups = nTotOzToDrink / 8.0;
  nTotCups = nTotCups.toPrecision(3);
  
  person.oz = nTotOzToDrink;
  person.cups = nTotCups;
  
  const ozInputNd = document.getElementById("oz"+nIndex);  
  ozInputNd.value = person.oz;
  
  const cupsInputNd = document.getElementById("cups"+nIndex);  
  cupsInputNd.value = person.cups;
  
} // end of function doCalc()


function updatePersonName(event) {
  const inp = event.target;
  const nIdx = inp.dataset.idx-0;
  const person = people[nIdx];
  person.name = inp.value;
  savePeople();
} // end of function 



function updatePersonAge(event) {
  const inp = event.target;
  const nIdx = inp.dataset.idx-0;
  const person = people[nIdx];
    
  person.age = inp.value - 0;
  doCalc(nIndex);
  savePeople();
} // end of function updatePersonAge()



function updatePersonWeight(event) {
  const inp = event.target;
  const nIdx = inp.dataset.idx-0;
  const person = people[nIdx];
    
  person.weight = inp.value - 0;
  doCalc(nIndex);
  savePeople();
  
} // end of function updatePersonWeight()



function updatePersonMinutes(events) {
  const inp = event.target;
  const nIdx = inp.dataset.idx-0;
  const person = people[nIdx];
    
  person.minutesExercise = inp.value - 0;
  doCalc(nIndex);
  savePeople();
} // end of function updatePersonMinutes()


function delPerson(event) {
  const btn = event.target;
  const nIdx = btn.dataset.idx-0;
  people.splice(nIdx,1);
  savePeople();
  displayPeople();
} // end of function delPerson()


function savePeople() {
  const sData = JSON.stringify(people);
  localStorage.setItem("people", sData);
} // end of function savePeople()


// make sure keyboard input is only of
// integer digits or the backspace character
// (Event handler for keydown event on integer text boxes)
function checkIntInput(event) {
  const kc = event.keyCode;
  const BACKSPACE = 8; // don't have to worry about the Tab key!

  let sChar = String.fromCharCode(kc);
  const sValidChars = "0123456789";

  // handle numbers entered from numeric keypad on keyboard:
  if (kc > 95 && kc < 106) {
    sChar = (kc - 96)+"";
  } // end if
  
  if (sValidChars.indexOf(sChar) === -1 && kc !== BACKSPACE) {
    // not a correct character... don't allow it to be accepted as part of the input!
    event.stopPropagation();
    event.preventDefault();  
    event.returnValue = false;
    event.cancelBubble = true;
    return false;
  } // end if

  
} // end of function checkIntInput()


/****************************************************************************************
   below was written because the Number.toPrecision() method does Not work quite the way 
   I need it to!!
 ****************************************************************************************/
function roundToPrecision(nNum,nDecPrec) {
  let sNum = nNum+"";
  let nPos = sNum.indexOf(".");
  
  if (nPos === -1) {
    // if no decimal we are done!
    return nNum;
  } // end if
  
  let nCurDecPrec = sNum.length - nPos - 1; // calc number's current decimal precision
  if (nCurDecPrec<=nDecPrec) {
    // actual precision of number is less than or equal to specified precision
    // so return the original number!
    return nNum;
  } // end if
  
} // end of function roundToPrecision()


window.addEventListener('load', pageSetup);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console