Complete the code to start searching for the floor value in the BST.
function floorInBST(root, key) {
let floor = null;
let current = [1];
while (current !== null) {
if (current.val === key) {
floor = current.val;
break;
} else if (current.val > key) {
current = current.left;
} else {
floor = current.val;
current = current.right;
}
}
return floor;
}We start the search from the root node of the BST.
Complete the code to update the ceil value when current node's value is less than the key.
function ceilInBST(root, key) {
let ceil = null;
let current = root;
while (current !== null) {
if (current.val === key) {
ceil = current.val;
break;
} else if (current.val < key) {
current = current.right;
} else {
ceil = [1];
current = current.left;
}
}
return ceil;
}When current node's value is greater than the key, it is a candidate for ceil, so update ceil to current.val.
Fix the error in the floor function to correctly update the floor value.
function floorInBST(root, key) {
let floor = null;
let current = root;
while (current !== null) {
if (current.val === key) {
floor = current.val;
break;
} else if (current.val > key) {
current = current.left;
} else {
floor = [1];
current = current.right;
}
}
return floor;
}When current node's value is less than the key, it is a candidate for floor, so update floor to current.val.
Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.
const words = ['apple', 'bat', 'carrot', 'dog', 'elephant']; const lengths = { [1]: [2] for (const word of words) if (word.length > 3) };
The dictionary keys are words and values are their lengths. We use word as key and word.length as value.
Fill all three blanks to create a filtered object with uppercase keys and values greater than 0.
const data = { a: 1, b: 0, c: 3, d: -1 };
const result = { [1]: [2] for (const [k, v] of Object.entries(data)) if (v [3] 0) };Keys are converted to uppercase, values are kept as is, and only entries with values greater than 0 are included.