Today I have solved the problem in javascript

 The United States uses the imperial system of weights and measures, which means that there are many different, seemingly arbitrary units to measure distance. There are 12 inches in a foot, 3 feet in a yard, 22 yards in a chain, and so on.


Create a data structure that can efficiently convert a certain quantity of one unit to the correct amount of any other unit. You should also allow for additional units to be added to the system.

function convert(quantity, fromUnit, toUnit) {

  if (!conversions[fromUnit] || !conversions[toUnit]) {

    return "Invalid units";

  }

  return quantity * conversions[fromUnit][toUnit];

}


const conversions = {

  inch: {

    foot: 1 / 12,

    yard: 1 / 36,

    mile: 1 / 63360,

    

  },

  foot: {

    inch: 12,

    yard: 1 / 3,

    mile: 1 / 5280,

    

  },

  yard: {

    inch: 36,

    foot: 3,

    mile: 1 / 1760,

    

  },

  mile: {

    inch: 63360,

    foot: 5280,

    yard: 1760,

   

  },

 };console.log(convert(10, 'inch', 'foot')); 

console.log(convert(2, 'mile', 'yard'));

Output:

0.8333333333333333
3520


Comments

Popular posts from this blog

Building a Full-Stack Student Management System with React.js and NestJS

Today I have solved the problem in javascript

Today I have solved problem in javascript