We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Break

The break keyword can be used to break out of a loop early.

for (let i = 0; i < 10; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}
// Prints:
// 0
// 1
// 2

You can omit the loop condition in a for loop to create an intentional infinite loop and then use break to exit, for example:

for (let i = 0; ; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}

No matter the end condition, when a break statement is encountered, the loop will exit immediately.

Assignment

Textio can send as many messages as the budget allows. We want to find out how many messages we can send before the total cost of those messages would exceed the given budget.

Complete the maxMessagesWithinBudget function. It should use an infinite loop to keep track of the totalCost and a count of the messages sent.