Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="container">
  <h1>Status:&nbsp;<span id="hunger-status">Full</span></h1>
  <div class="hunger-bar-container">
    <div class="hunger-bar" id="hunger-bar">
    </div>
  </div>
  <div class="buttons">
     <button onclick="eatFood(trout)">Eat Fish</button>
   <button onclick="eatFood(beans)">Eat Beans</button>
   <button onclick="eatFood(deer)">Eat Deer</button>
    <button onclick="eatFood(granola_bar)">Eat Granola Bar</button>
  </div>

</div>

              
            
!

CSS

              
                .hunger-bar-container {
  height: 20px;
  width: 500px;
  border: 1px solid black;
  margin: 0 auto;
}
.hunger-bar {
  background-color: green;
  height: 20px;
}

button {
  background-color: white;
  border: 1px solid black;
  padding: 10px;
  font-size: 16px;
}

.buttons {
  margin: 0 auto;
  text-align: center;
  margin-top: 20px;
}

h1 {
   text-align: center;
}

              
            
!

JS

              
                const maxCalories = 2500; // maximum calories you can eat
let currentCalories = 2500; //calories you need to eat per day
let dead = false;
const trout = 150;
const deer = 800;
const granola_bar = 200;
const beans = 450;
let fullness = 70; // still keeping percentage for display purposes
let hungerBar = document.getElementById("hunger-bar");
let hungerStatus = document.getElementById("hunger-status");
hungerBar.style.width = fullness + "%";
 
setInterval(function() { 
    currentCalories = currentCalories- 60; //subtract fullness by 60 because we burn 60 calories per hour while sitting
   updateFullness();
}, 30000); // 1000 is 1 second (in milliseconds) so we are running this code every 30 seconds


function updateFullness() {
    if((currentCalories/maxCalories) * 100 <= 100 ) {
     fullness = (currentCalories/maxCalories) * 100 //calculate fullness percentage
    } else {
    fullness = 100;
    }
    
    if (fullness < 70) {
     hungerBar.style.backgroundColor = "#ccaa2b";
     hungerStatus.innerHTML = "Hungry";
    }
    
   if (fullness < 30) {
    hungerStatus.innerHTML = "Starving";
    hungerBar.style.backgroundColor = "red";
   }
  
  
  if (fullness <= 0) {
     dead = true;
     hungerStatus.innerHTML = "Dead";
  }
    hungerBar.style.width = fullness + "%";
}

function eatFood(food) {
  if (!dead) {
     currentCalories = currentCalories + food;
     updateFullness();
  }
}
              
            
!
999px

Console