Build a Digital Pet Game - Build a Digital Pet Game

Tell us what’s happening:

“Guys, I’ve tried many times but it’s still failing tests 4–11 and 15–29.”

Your code so far

<!-- file: index.html -->

/* file: index.tsx */
const { useState, useEffect } = React;
             
export const PetGame = () => {
const [name,setName] =  useState("")
  return (
    <div  className="game-container">
    <header>
    <h1>Digital Pet Game</h1>
<p>Take care of your virtual companion!</p>
    </header> 
     
{!name ? <FormElement setName={setName}/>:  
   <ContainerDiv name={name}/>
}
    </div>
  ) 
};

const FormElement = ({setName}:{setName:(value:string)=>void})=>{
  const [values,setValue] = useState("");

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault(); 
    if (!values.trim()) {
      alert("Please provide a name");
      return;
    }

    setName(values.trim());
  };
  return (
<div>
<form className="form" onSubmit={handleSubmit}>
<label htmlFor="pet-name"> Enter the name of your pet</label>
<input type="text" id="pet-name" onChange={(e)=>{setValue(e.target.value)}} value={values}/>
<button id="set-name-btn" type="submit">Start Game</button> 
</form>
</div>
  
  )
}
const ContainerDiv = ({name}:{name:string})=>{
enum PetMood {
  HAPPY,
  EXCITED,
  CONTENT,
  SAD,
  TIRED,
  SICK,
  HUNGRY
}
const [hunger,setHunger] = useState(0);
const[energy,setEnergy] = useState(100);
const [happiness,setHappiness] = useState(100);
const obj: Record<PetMood, string> = {
   [PetMood.HAPPY]: "😊",
  [PetMood.EXCITED]: "🤩",
  [PetMood.CONTENT]: "😌",
  [PetMood.SAD]: "😢",
  [PetMood.TIRED]: "😴",
  [PetMood.SICK]: "🤢",
  [PetMood.HUNGRY]: "🍖",
};
function getMood(
  hunger: number,
  happiness: number,
  energy: number
): string {
  if (hunger > 70) return obj[PetMood.HUNGRY];
  if (energy < 30) return obj[PetMood.TIRED];
  if (happiness > 80 && energy > 70) return obj[PetMood.EXCITED];
  if (happiness > 60) return obj[PetMood.HAPPY];

  return obj[PetMood.CONTENT];
}
function getStatus(value:number):string{

  if(value>70) return "high";
  if(value>40) return "medium";
  return "low"
}
function getStausForHunger(value:number):string{
  if(value<40) return "high"

  if(value<70) return "medium"
return "low"
}

useEffect(() => {
  const timer = setTimeout(() => {
    
     setEnergy(prev => Math.max(0, prev - 5));
    setHappiness(prev => Math.max(0, prev - 5));
     setHunger(prev => Math.min(100, prev + 5));
  }, 12000);
 
  return () => clearTimeout(timer);
}, [hunger, energy, happiness]);
return (
<section>
<div className="base-container">
<p className="pet-name">{name}</p>
<p className="pet-sprite">{getMood(hunger,happiness,energy)}</p>
<div className="pet-buttons">
<button id="eat-action" className="pet-button"
onClick={
  ()=>{
      setHunger(prev => Math.max(0, prev - 5));
    setEnergy(prev => Math.min(100, prev + 5));
  }
}
>Eat</button>
<button className="pet-button" id="play-action" onClick={
  ()=>{
     setEnergy(prev => Math.max(0, prev - 5));
    setHappiness(prev => Math.min(100, prev + 5));
  }
}>Play</button>
<button className="pet-button" id="sleep-action" onClick={
  ()=>{
     setHunger(prev => Math.min(100, prev + 5));
    setEnergy(prev => Math.min(100, prev + 5));
  }
}>Sleep</button>
</div>
</div>

<div className="stats-grid">
<article className="stat-bar stat">
<div className="stat-header">
<div className="stat-icon" aria-hidden>
🍽️
</div>

<p className="stat-name">Hunger</p>
<p className="stat-value">{hunger}%</p>
</div>
<div className={`stat-progress`}>
<div className={`stat-fill ${getStausForHunger(hunger)}`} style={{width:`${hunger}%`}}></div>
</div>
</article>  

<article className="stat-bar stat">
<div className="stat-header">
<div className="stat-icon" aria-hidden>😊
</div>
<p className="stat-name">Happiness</p>
<p className="stat-value">{happiness}%</p>
</div>
<div className={`stat-progress`}>
<div className={`stat-fill ${getStatus(happiness)}`} style={{width:`${happiness}%`}}></div>
</div>
</article>

<article className="stat-bar stat">
<div className="stat-header">
<div className="stat-icon" aria-hidden>⚡
</div>
<p className="stat-name">Energy</p>
<p className="stat-value">{energy}%</p>
</div>
<div className={`stat-progress`}>
<div className={`stat-fill ${getStatus(energy)}`} style={{width:`${energy}%`}}></div>
</div>
</article>

</div>
<div id="pet-cat" className="base-container">
<h2>Species: Cat</h2>
<p>Pet Fact: It has been said that the Ukrainian Levkoy has the appearance of a dog, due to the angles of its face</p>
</div>

</section>
)


}
/* file: styles.css */
:root {
  /* Backgrounds */
  --bg-gradient-start: #dbeafe;
  --bg-gradient-mid: #faf5ff;
  --bg-gradient-end: #fce7f3;

  /* Text */
  --text-primary: #1f2937;
  --text-muted: #6b7280;
  --text-muted-2: #9ca3af;

  /* Card / surface */
  --card-bg-translucent: rgba(255, 255, 255, 0.6);
  --card-bg-solid: rgba(255, 255, 255, 0.8);

  /* Accent / brand */
  --accent-purple: #8b5cf6;
  --accent-purple-dark: #7c3aed;

  /* Actions */
  --action-feed: #10b981;
  --action-feed-dark: #059669;
  --action-play: #3b82f6;
  --action-play-dark: #2563eb;
  --action-sleep: #8b5cf6;
  --action-sleep-dark: #7c3aed;
  --action-heal: #ec4899;
  --action-heal-dark: #db2777;

  /* Stats */
  --stat-high: #10b981;
  --stat-medium: #f59e0b;
  --stat-low: #ef4444;

  /* UI greys */
  --ui-bg-light: #e5e7eb;
  --ui-fill: #8b5cf6; /* used for exp fill as accent */

  /* Shadows */
  --shadow-strong: rgba(0, 0, 0, 0.25);
  --shadow-soft: rgba(0, 0, 0, 0.1);
}

html {
  /*color-scheme: light dark;*/
  font-family: system-ui;
  display: flex;
  justify-content: center;
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu",
    "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background: linear-gradient(135deg, #dbeafe 0%, #faf5ff 50%, #fce7f3 100%);
  min-height: 100vh;
  padding: 1rem;
}

header {
  text-align: center;
  margin-bottom: 2rem;
}

body {
  max-width: 400px;
}

.base-container {
  background: rgba(255, 255, 255, 0.6);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border-radius: 1.5rem;
  padding: 2rem;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  .pet-name {
    text-align: center;
  }
}

.game-container {
  margin-bottom: 1.5rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

/* Stats Grid */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-bar {
  background: rgba(255, 255, 255, 0.8);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border-radius: 0.75rem;
  padding: 1rem;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.2s ease;
}

.stat-bar:hover {
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.stat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.stat-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.stat-icon {
  font-size: 1.25rem;
}

.stat-name {
  font-weight: 500;
  color: #374151;
}

.stat-value {
  font-weight: bold;
  color: #1f2937;
}

.stat-progress{
  width: 100%;
  height: 0.75rem;
  background: #e5e7eb;
  border-radius: 9999px;
  overflow: hidden;
}

.stat-fill{ 
  height: 100%;
  transition: width 0.5s ease-out;
  border-radius: 9999px;
}

.stat-fill.high {
  background: #10b981;
}

.stat-fill.medium {
  background: #f59e0b;
}

.stat-fill.low {
  background: #ef4444;
}

.pet-buttons {
  width: auto;
  display: flex;
  flex-direction: row;
  gap: 1rem;
}

.pet-buttons .pet-button {
  min-width: 80px;
  background: #c2214a;
  color: white;
  border-radius: 13%;
  border-style: solid;
  border-color: #870b04;
  box-shadow: rgba(120, 25, 23, 0.2) 2px 2px 4px 2px;
  &:active {
    transform: scale(0.95);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15) inset;
  }
  text-align: center;
  cursor: pointer;
  padding: 0.5rem 1rem;
}

.pet-sprite {
  font-size: xxx-large;
  text-align: center;
}

.data-management * {
  margin: 0 0.5rem 0 0;
}

.info-panel {
  text-align: center;
}
.info-panel #hud,
.info-panel .start-questions {
  display: flex;
  align-items: center;
  flex-direction: column;
  gap: 1rem;
}

.info-panel #hud p {
  margin: 0rem;
}

.info-panel .start-questions div * {
  margin: 0 0.5rem 0 0;
}

#pet-fact {
    background: rgba(255, 255, 255, 0.6);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border-radius: 1.5rem;
  padding: 2rem;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  color: var(--text-muted-2);
  margin: 0 0 0.5rem 0;
}
#set-name-btn {
  display: inline-block;
  padding: 14px 28px;
  font-size: 1.15rem;
  font-weight: 600;
  background: linear-gradient(135deg, #6b8cff, #5ec2ff);
  color: #fff;
  border: none;
  border-radius: 10px;
  box-shadow: 0 6px 18px rgba(92, 132, 255, 0.25);
  cursor: pointer;
  transition:
    transform 0.08s ease,
    box-shadow 0.12s ease,
    opacity 0.12s;
}

#set-name-btn:hover {
  opacity: 0.98;
}

#set-name-btn:active,
#set-name-btn.pressed {
  transform: translateY(2px) scale(0.995);
  box-shadow: 0 3px 8px rgba(92, 132, 255, 0.18);
}

#set-name-btn:focus {
  outline: 3px solid rgba(94, 190, 255, 0.28);
  outline-offset: 2px;
}
.form{
  background-color: white;
  padding:1rem;
  display:flex;
  flex-direction:column;
  justify-content:space-between;
  gap:1rem;
  border-radius: 12px;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.0.0.0 Safari/537.36

Challenge Information:

Build a Digital Pet Game - Build a Digital Pet Game

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-digital-pet-game/68c362b379059c388d3874f2.md at main · freeCodeCamp/freeCodeCamp · GitHub