Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Integer Average

Fundamentals: Part 1medium

Course · Section 2: JavaScript Fundamentals – Part 1 · Lecture 13: Basic Operators

Implement `avg(a, b)` returning the floor of the average of two non-negative integers.

Sample tests

Input: avg(4, 8)
Output: 6
Input: avg(3, 4)
Output: 3

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Integer overflow is not a concern in JS doubles for normal values, but floating division still needs flooring.

Learning resources

  • MDN: Expressions and operators
Visualize this concept: Operator Precedence and Associativity →
Approach & explanation (try first)

Math.floor((a + b) / 2) gives the integer average of two non-negative numbers.

Loading...
⌘/Ctrl + Enter

Run your code to see results.