Skip to content

Instantly share code, notes, and snippets.

@alejandrolechuga
Created December 16, 2021 07:33
Show Gist options
  • Save alejandrolechuga/57e28ddc8e1ec195ee514c42e3ae642c to your computer and use it in GitHub Desktop.
Save alejandrolechuga/57e28ddc8e1ec195ee514c42e3ae642c to your computer and use it in GitHub Desktop.
// clamp
// 10 al 100
// 9 -> 10
// 199 -> 100
// 50 -> 50
// min 5
// max 100
function limitarCompra(numArticulos) {
let min = 5;
let max = 100;
return clamp(numArticulos, min, max);
}
function clamp(num, min, max) {
return Math.min(Math.max(num, min), max);
}
console.log(limitarCompra(2)); // 5
console.log(limitarCompra(40)); // 40
console.log(limitarCompra(199)); // 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment