Kodewick
Start learning →
Mini challenge←→0 / 60

Lives left

#015Mini challengeLoopsStrings6 min

A little game starts with 3 lives. Each event: "hit" loses one, "heart" gains one, anything else changes nothing. Once lives reach 0 the game is over and later events don't count, so return 0. Write lives_left(events) to return how many lives are left.

Examples
lives_left({"hit", "heart", "hit"})→2
lives_left({"hit", "hit", "hit", "heart"})→0
The functionint lives_left(const vector<string> &events)
main.cpp
#include <string>
#include <vector>
using namespace std;

int lives_left(const vector<string> &events) {
    int lives = 3;
    for (const string &e : events) {
        // your code here: add or take a life for e, and return 0 once it reaches 0
    }
    return lives;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.