📖 Scenario: You are working on a search feature for an online bookstore. When a user searches for a price, you want to find the closest book prices available in the store that are just below or equal to the searched price (floor), and just above or equal to the searched price (ceil).
🎯 Goal: Build a program that finds the floor and ceil values for a given target price in a sorted array of book prices.
📋 What You'll Learn
Create a sorted array of book prices
Create a target price variable
Write logic to find floor and ceil values for the target price
Print the floor and ceil values
💡 Why This Matters
🌍 Real World
Finding floor and ceil values is useful in pricing, searching, and recommendation systems where you want to find closest matches to a target value.
💼 Career
Understanding floor and ceil concepts helps in roles like software development, data analysis, and algorithm design where efficient searching and data handling are required.
Progress0 / 4 steps
1
Create a sorted array of book prices
Create a sorted array called bookPrices with these exact values: [10, 20, 30, 40, 50, 60, 70]
DSA Javascript
Hint
Use const to create the array and include all values in ascending order.
2
Create a target price variable
Create a variable called targetPrice and set it to 35
DSA Javascript
Hint
Use const to create the variable and assign the value 35.
3
Find floor and ceil values for the target price
Write a function called findFloorAndCeil that takes arr and target as parameters and returns an object with floor and ceil properties. Use a for loop with variable i to iterate over arr. Inside the loop, update floor to the largest value less than or equal to target, and ceil to the smallest value greater than or equal to target. Initialize floor to null and ceil to null before the loop.
DSA Javascript
Hint
Use a loop to check each value. Update floor if the value is less than or equal to target. Update ceil if the value is greater than or equal to target and less than current ceil.
4
Print the floor and ceil values
Call the findFloorAndCeil function with bookPrices and targetPrice, store the result in result, then print Floor: followed by result.floor and Ceil: followed by result.ceil using two separate console.log statements.
DSA Javascript
Hint
Store the returned object in a variable and use console.log to print floor and ceil values on separate lines.
Practice
(1/5)
1. What does the floor of a number mean in a sorted array context?
easy
A. The greatest element less than or equal to the number
B. The smallest element greater than or equal to the number
C. The average of all elements less than the number
D. The smallest element strictly less than the number
Solution
Step 1: Understand floor definition in sorted array
The floor of a number is the greatest element in the array that is less than or equal to the given number.
Step 2: Compare with options
The greatest element less than or equal to the number matches this definition exactly, while others describe ceil or incorrect concepts.
Final Answer:
The greatest element less than or equal to the number -> Option A
Quick Check:
Floor = greatest ≤ number [OK]
Hint: Floor is always ≤ number, pick greatest such element [OK]
Common Mistakes:
Confusing floor with ceil
Thinking floor must be strictly less than number
Mixing floor with average or median
2. Which of the following is the correct JavaScript syntax to find the floor of a number x in a sorted array arr using binary search?
easy
A. Use Math.floor(x) directly on array
B. Use a for loop from 0 to arr.length and return first element greater than x
C. Use arr.indexOf(x) to find floor
D. Use a while loop with start = 0, end = arr.length - 1, update mid and check arr[mid] <= x
Solution
Step 1: Understand binary search for floor
Binary search uses start and end pointers, calculates mid, and checks if arr[mid] <= x to move start or end accordingly.
Step 2: Evaluate options
Use a while loop with start = 0, end = arr.length - 1, update mid and check arr[mid] <= x correctly describes binary search logic for floor. Options A, B, and C are incorrect or irrelevant.
Final Answer:
Use a while loop with start=0, end=arr.length-1, update mid and check arr[mid] ≤ x -> Option D
Quick Check:
Binary search with arr[mid] ≤ x finds floor [OK]
Hint: Binary search with arr[mid] ≤ x finds floor efficiently [OK]
Common Mistakes:
Using linear search instead of binary search
Using indexOf which only finds exact matches
Misusing Math.floor which works on numbers, not arrays
3. What is the output of the following JavaScript code?
const arr = [1, 3, 5, 7, 9];
function findFloor(arr, x) {
let start = 0, end = arr.length - 1, floor = -1;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (arr[mid] <= x) {
floor = arr[mid];
start = mid + 1;
} else {
end = mid - 1;
}
}
return floor;
}
console.log(findFloor(arr, 6));