jsartisan / frontend-challenges

FrontendChallenges is a collection of frontend interview questions and answers. It is designed to help you prepare for frontend interviews. It's free and open source.
https://frontend-challenges.com
27 stars 4 forks source link

clamp #148

Closed jsartisan closed 1 month ago

jsartisan commented 1 month ago

Info

difficulty: easy
title: clamp
type: question
template: typescript
tags: javascript

Question

Implement a function similar to _.clamp from the Lodash library. The function clamp takes three arguments: a number, a lower bound, and an upper bound. The function returns the number clamped within the inclusive lower and upper bounds.

Example usage

console.log(clamp(10, 5, 15)); // 10
console.log(clamp(4, 5, 15)); // 5
console.log(clamp(20, 5, 15)); // 15

Template

index.ts

export function clamp(number: number, lower: number, upper: number): number {

}

index.test.ts

import { clamp } from './index';

describe('clamp function', () => {
  test('should return the number if it is within the bounds', () => {
    expect(clamp(10, 5, 15)).toBe(10);
  });

  test('should return the lower bound if the number is less than the lower bound', () => {
    expect(clamp(4, 5, 15)).toBe(5);
  });

  test('should return the upper bound if the number is greater than the upper bound', () => {
    expect(clamp(20, 5, 15)).toBe(15);
  });

  test('should handle negative bounds', () => {
    expect(clamp(-10, -5, 5)).toBe(-5);
    expect(clamp(10, -5, 5)).toBe(5);
    expect(clamp(0, -5, 5)).toBe(0);
  });

  test('should handle bounds being equal', () => {
    expect(clamp(10, 10, 10)).toBe(10);
    expect(clamp(5, 5, 5)).toBe(5);
  });

  test('should handle bounds where lower > upper by returning lower', () => {
    expect(clamp(10, 15, 5)).toBe(15);
    expect(clamp(3, 5, 3)).toBe(5);
  });
});
github-actions[bot] commented 1 month ago

149 - Pull Request updated.