- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the height based on width and screen size ratio (width:height) in JavaScript
Problem
We are required to write a JavaScript function that takes in the width of the screen as the first argument and the aspect ratio (w:h) as the second argument. Based on these two inputs our function should return the height of the screen.
Example
Following is the code −
const ratio = '18:11'; const width = 2417; const findHeight = (ratio = '', width = 1) => { const [w, h] = ratio .split(':') .map(Number); const height = (width * h) / w; return Math.round(height); }; console.log(findHeight(ratio, width));
Output
Following is the console output −
1477
- Related Articles
- Changing Column Width Based on Screen Size using CSS
- How to get the Width and Height of the screen in JavaScript?
- Changing Layouts Based on Screen Size using CSS
- Finding quarter based on month index in JavaScript
- Finding length, width and height of the sheet required to cover some area using JavaScript
- Finding number plate based on registration number in JavaScript
- Finding astrological signs based on birthdates using JavaScript
- How can I get android device screen height, width?
- Finding life path number based on a date of birth in JavaScript
- HTML5 Canvas Font Size Based on Canvas Size
- The width and height properties in CSS
- Get the size of the screen, current web page and browser window in JavaScript
- Hide content depending on screen size with Bootstrap
- HTML Screen width Property
- Filtering Images based on size attributes in Python?

Advertisements