---
# System prepended metadata

title: Untitled

---

## Tutor - /clear

- Method: `DELETE`
- Parameter Type: `Query`

```ts
app.delete('/clear', (req: Request, res: Response) => {
    res.json(clear());
});
```

---

## Group AERO - /addnameage

- Method: put
- Parameter Type: 
- 
```ts
console.log('TODO')
```

---

## Group BOOST - /getnamesages

- Method: GET (read)
- Parameter Type: requires query input parameter

const app = express();

app.get('/getnamesages', (req: Request, res: Response) => {
    const minAge
    res.json(getnamesages(minAge))



```ts
console.log('TODO')
```

---

## Group CRUNCHIE - /editnameage

- Method: PUT
- Parameter Type: request


app.get('/editNameAge', (req: Request, res: Response) => {
   const { name, age } = req.body;
   req.json(editNameAge(name, age));
})

---

## Group DREAM - /removenameage

- Method: delete
- Parameter Type: 

```Returns error if name string does not exist in data base and removes it if it is. i```
app.delete('/removenameage', (req: Request, res: Response) => {
    const name = req.query.name as string;
    res.json(removeNameAge(name));
});

---

## Group EGGS - /getstats

- Method: Get
- Parameter Type: res, req

app.get("/getstats", (req: Request, res: Response) => {
    const response = res.json(getStats());
});  


```ts
console.log('TODO')
```

---
```ts
interface NameAge {
  name: string,
  age: number,
}

let namesAges: NameAge[] = [];

const findNameAge = (name: string): NameAge | undefined => {
  for (const nameAge of namesAges) {
    if (nameAge.name === name) {
      return nameAge;
    }
  }
};

export function clear() {
  namesAges = [];
  return {};
}

export function addNameAge(name: string, age: number) {
  if (!name || findNameAge(name) || age <= 0) {
    return { error: 'Name is empty, name already exists or age is not strictly positive.' };
  }
  namesAges.push({ name, age });
  return {};
}

export function getNamesAges(minAge?: number) {
  let copy: NameAge[];
  if (minAge !== undefined) {
    if (minAge <= 0) {
      return { error: 'minAge exists and is not strictly positive' };
    }
    copy = namesAges.filter(nameAge => nameAge.age >= minAge);
  } else {
    copy = [...namesAges];
  }
  copy.sort((a: NameAge, b: NameAge) => {
    if (a.age !== b.age) {
      return b.age - a.age;
    }
    return a.name.localeCompare(b.name);
  });
  return { namesAges: copy };
}

export function editNameAge(name: string, age: number) {
  const nameAge = findNameAge(name);
  if (!nameAge || age <= 0) {
    return { error: 'name does not exist or age is not strictly positive' };
  }
  nameAge.age = age;
  return {};
}

export function removeNameAge(name: string) {
  if (!findNameAge(name)) {
    return { error: 'name does not exist' };
  }
  namesAges = namesAges.filter(nameAge => nameAge.name !== name);
  return {};
}

export function getStats() {
  if (namesAges.length === 0) {
    return { error: 'no entries available in the data store' };
  }
  return {
    stats: {
      minAge: Math.min(...namesAges.map(nameAge => nameAge.age)),
      maxAge: Math.max(...namesAges.map(nameAge => nameAge.age)),
      averageAge: namesAges.reduce((sum, nameAge) => sum + nameAge.age, 0) / namesAges.length,
    }
  };
}

```

---






































































