JavaScript bootcamp · Lab 41

Safe divide

easyErrors10 minLesson: Errors

Read the question, write JavaScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read a and b. Log a/b if b ≠ 0, else error.

Input. Two numbers.

Output. Quotient or error.

Examples

Example 1
Input
10 2
Output
5
Example 2
Input
10 0
Output
error
Hint
  1. Guard b === 0 before dividing.
Show correct code

Peek only after you have tried. You can still Check your own version.

const [a, b] = readLine().trim().split(/\s+/).map(Number);
console.log(b === 0 ? 'error' : a / b);
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.