- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Converting humanYears into catYears and dogYears in JavaScript
Problem
We are required to write a JavaScript function that takes in human age in years and returns respective dogYears and catYears.
Input
const humanYears = 15;
Output
const output = [ 15, 76, 89 ];
Example
Following is the code −
const humanYears = 15; const humanYearsCatYearsDogYears = (humanYears) => { let catYears = 0; let dogYears = 0; for (let i = 1; i <= humanYears; i++) { if (i === 1) { catYears += 15; dogYears += 15; }else if (i === 2) { catYears += 9; dogYears += 9; }else { catYears += 4; dogYears += 5; } } return [humanYears, catYears, dogYears]; }; console.log(humanYearsCatYearsDogYears(humanYears));
Output
[ 15, 76, 89 ]
Advertisements