Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!-- displays site properly based on user's device -->

    <link
      rel="icon"
      type="image/png"
      sizes="32x32"
      href="./images/favicon-32x32.png"
    />
    <link rel="stylesheet" href="styles.css" />

    <title>Frontend Mentor | Tip calculator app</title>

    <!-- Feel free to remove these styles or customise in your own stylesheet 👍 -->
    <style>
      .attribution {
        font-size: 11px;
        text-align: center;
      }
      .attribution a {
        color: hsl(228, 45%, 44%);
      }
    </style>
  </head>
  <body>
    <div class='title'>
    <p>SPLI</p>
    <p>TTER</p>
    </div>
    <div class="tip-flex-container">
      <div class="calculating-bill">
        <p class="inline">Bill</p>
        <input type="text" id="amount" placeholder="0" required />

        <div class="tip-amount">
          <p>Select Tip %</p>
          <button value=".5" class="percent">5%</button>
          <button value=".10" class="percent">10%</button>
          <button value=".15" class="percent">15%</button>
          <button value=".20" class="percent">20%</button>
          <button value=".25" class="percent">25%</button>
          <input placeholder="Custom" class="percent" id="custom"></input>
        </div>
        <div class="people">
          <p class="inline">Number of People</p>
          <input type="text" placeholder="0" id="people" required />
          <button class="calculate">Calculate Total</button>
        </div>
      </div>
      <div class="calculating-tip">
        <div class="tip-total">
          <p>Tip Amount</p>
          <p>/ person</p>
          <p id="tipA">$0.00</p>
        </div>
        <div class="tip-total">
          <p>Total</p>
          <p>/ person</p>
          <p id="total">$0.00</p>
        </div>

        <button class="reset">RESET</button>
      </div>
    </div>

    <div class="attribution">
      Challenge by
      <a href="https://www.frontendmentor.io?ref=challenge" target="_blank"
        >Frontend Mentor</a
      >. Coded by <a href="#">Your Name Here</a>.
    </div>
    <script src="index.js"></script>
  </body>
</html>

              
            
!

CSS

              
                * {
  box-sizing: border-box;
  margin: 0px;
  padding: 0px;
}
body {
  background-color: #c4e4e7;
}
.title{
  text-align: center;
  letter-spacing: 6.5px;
  color:#01464B;
  font-size: 30px;
}
.tip-flex-container {
  display: flex;
  flex-direction: row;
  height: 600px;
  align-items: center;
  justify-content: center;
  border-radius: 20px;
  background: white;
  width: 900px;
  margin: auto;
  margin-top: 3rem;
}
.people span {
  color: red;
  font-weight: bold;
  margin-right: 100px;
}
.calculating-bill span {
  padding-left: 223px;
  font-weight: bold;
  color: red;
}
.calculating-bill {
  width: 400px;
  height: 500px;
  flex-wrap: wrap;
  padding: 30px;
}
#amount {
  text-align: right;
  background-image: url("https://i.ibb.co/Q7VNLgF/icon-dollar-1.png");
  background-repeat: no-repeat;
  background-color: #f3f8fc;
  background-position: center left;
  color: #00474b;
  font-size: 15px;
  width: 100%;
  height: 30px;
}
input:focus {
  outline-style: solid;
  outline-color: #c4e4e7;
}
.tip-total p:first-child {
  color: white;
}
.tip-total p:nth-child(2) {
  color: #26c2ad;
}
#tipA,
#total {
  color: #26c2ad;
  font-size: 30px;
  font-weight: bold;
  padding-top: 15px;
}
button {
  width: 100px;
  padding: 10px;
  margin: 5px;
  background-color: #01464b;
  color: white;
  border-radius: 5px;
  height: 50px;
  font-size: 20px;
}
button:hover {
  background-color: #28c1ad;
}
button:active {
  background-color: #9fe8df;
  color: #01464b;
}
.tip-amount {
  padding-top: 20px;
  padding-bottom: 20px;
}
#people {
  background-image: url("https://i.ibb.co/27jR8dM/icon-person.png");
  background-repeat: no-repeat;
  background-position: center left;
  background-color: #f3f8fc;
  text-align: right;
  width: 100%;
  height: 30px;
}
#inline {
  display: inline;
}
.calculate {
  display: block;
  margin-top: 20px;
}
.calculating-tip {
  display: flex;
  flex-direction: column;
  border: 1px solid blue;
  width: 400px;
  height: 500px;
  background-color: #01464b;
  border-radius: 20px;
  padding: 20px;
}
.tip-total {
  margin: 50px;
}
#custom{
  width: 100px;
  border-radius: 5px;
  height: 50px;
  background-color: #f3f8fc;
  color: #01464b;
  padding: 5px;
  margin-left: 5px;
  font-size: 25px;
  font-weight:bold;
}
#custom:hover{
  cursor:pointer;
}
.inline {
  display: inline;
}
.reset {
  width: 100%;
  border-radius: 10px;
  background-color: #25c2ae;
  color: #01464b;
  font-size: 20px;
  font-weight: bold;
}
@media only screen and (max-width: 600px){
  .tip-flex-container{
    flex-direction: column;
    height: 800px;
  }
  button{
    width: 200px
  }
  .calculating-bill {
    margin-right: 500px;
  }
  .calculating-tip {
    
  }
  .tip-total{
    margin: 5px;
  }
}

              
            
!

JS

              
                const billTotal = document.querySelector("#amount");
const cal = document.querySelector(".calculate");
const people = document.querySelector("#people");
const tipA = document.querySelector("#tipA");
const totalPer = document.querySelector("#total");
const custom = document.querySelector("#custom");

let customTipParse;
let percentValue;
let percentParse;
let billParse;
let peopleParse;
let tipPer;
let tipPerPerson;
let decimalTip;

const getTip = () => {
  const percentButton = document
    .querySelectorAll(".percent")
    .forEach((item) => {
      item.addEventListener("click", () => {
        percentValue = item.value;
        percentParse = Number(percentValue);
      });
    });
  return percentParse;
};

const calculateTotal = () => {
  cal.addEventListener("click", () => {
    tipChoice();
    inputBillCheck();
    inputPeopleCheck();
  });
};

const customTip = () => {
  const custom = document.querySelector("#custom").addEventListener("blur", () => {
    const customTipp = document.querySelector("#custom").value;
    decimalTip = Number(customTipp);
    percentParse = decimalTip/100;
    
   });
  return percentParse;
}

const inputPeopleCheck = () => {
  const numPeople = people.value;
  peopleParse = Number(numPeople);
  if (peopleParse === 0) {
    if (!document.querySelector(".people span")) {
      const span = document.createElement("span");
      span.innerText = "Can not be 0";
      const people = document.querySelector(".people");
      people.insertBefore(span, people.childNodes[2]);
      tipA.innerHTML = "Please see server";
      totalPer.innerHTML = "Please see server";
    }
  }
};

const inputBillCheck = () => {
  const bill = billTotal.value;
  billParse = Number(bill);
  if (billParse === 0) {
    if (!document.querySelector(".calculating-bill span")) {
      const span = document.createElement("span");
      span.innerText = "Can not be 0";
      const billErr = document.querySelector(".calculating-bill");
      billErr.insertBefore(span, billErr.childNodes[2]);
      tipA.innerHTML = "Please see server";
      totalPer.innerHTML = "Please see server";
    }
  }
};

const tipChoice =  () => {
  getTip();
  customTip();
   
    let bill = billTotal.value;
    billParse = Number(bill);

    let numPeople = people.value;
    peopleParse = Number(numPeople);
    console.log("people", peopleParse);

    let tip = billParse * percentParse;
    tipPer = tip / peopleParse;
    tipPerPerson = tipPer.toFixed(2);

    let total = billParse + tip;
    let perPerson = total / peopleParse;
    let totalPerPerson = perPerson.toFixed(2);
    console.log("tip", percentParse);
    tipA.innerHTML = "$ " + tipPerPerson;
    totalPer.innerHTML = "$ " + totalPerPerson;
    console.log(typeof tip);
    console.log("button clicked");
  console.log("tip", percentParse);
}
  
const resetInput = () => {
  const reset = document
    .querySelector(".reset")
    .addEventListener("click", () => {
      tipA.innerHTML = "$0.00";
      totalPer.innerHTML = "$0.00";
      people.value = 0;
      amount.value = 0;
      custom.value = 0;
      const span = document.querySelectorAll("span").forEach((item) => {
        item.remove();
      });
    });
};
calculateTotal();
resetInput();
console.log(getTip());
console.log(customTip());

              
            
!
999px

Console