Skip to content

Instantly share code, notes, and snippets.

@designviacode
Created July 24, 2016 23:05
Show Gist options
  • Save designviacode/ebd53769b2f682cd32a1069dc99308e4 to your computer and use it in GitHub Desktop.
Save designviacode/ebd53769b2f682cd32a1069dc99308e4 to your computer and use it in GitHub Desktop.
Scale the Aspect ratio of an image to fit inside certain box of 200 x 200 [currently default]
<script id="jsbin-source-javascript" type="text/javascript">
/* jshint esversion: 6 */
let windowSize = 200;
const resize_dimension_aspect_ratio = (w, h) => {
let newValues = [];
let aspectRatio = 0;
if (w > h) {
aspectRatio = Math.round((h / w) * windowSize);
newValues.push(windowSize, aspectRatio);
} else {
aspectRatio = Math.round((w / h) * windowSize);
newValues.push(aspectRatio, windowSize);
}
return newValues;
};
console.clear();
// Challenge Problem
console.log("Create a function that will scale the aspect ratio of image dimension ([w,h]) to fit inside of a 200x200 box by using scale to fit. Meaning that one side, the width or height, will be 200 and the other will maintain the aspect ratio.");
// Examples
console.log(resize_dimension_aspect_ratio(1256, 1200));
console.log(resize_dimension_aspect_ratio(600, 800));
console.log(resize_dimension_aspect_ratio(200, 200));
console.log(resize_dimension_aspect_ratio(400, 200));
console.log(resize_dimension_aspect_ratio(800, 1256));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment