diff --git a/L-B/0027 Remove duplicates/README.md b/L-B/0027 Remove duplicates/README.md new file mode 100644 index 0000000..5e79830 --- /dev/null +++ b/L-B/0027 Remove duplicates/README.md @@ -0,0 +1,62 @@ +# 0027 removeDuplicates ( L-B ) + +## Problem +Write a function that removes duplicate values from the given input. +- If the input is an array, return a new array with unique values. +- If the input is a string, return a new string with duplicate characters removed. +- The original order must be preserved. + +## Solution +```js +function removeDuplicates(input) { + let unique = []; + for (let i = 0; i < input.length; i++) { + if (!unique.includes(input[i])) { + unique.push(input[i]); + } + } + if(typeof(input) === "string"){ + unique = unique.toString().replace(/,/g, ''); + } + console.log(unique); + return unique; +} +``` + +## Test Cases +```js +removeDuplicates([2, 4, 8, 4, 2, 6, 9, 2, 6, 8, 10]); +// Output: [2, 4, 8, 6, 9, 10] + +removeDuplicates([1, 1, 1, 4, 0, 6, -2, 2, 6, 7, 10]); +// Output: [1, 4, 0, 6, -2, 2, 7, 10] + +removeDuplicates("zoom"); +// Output: "zom" + +removeDuplicates("hello world"); +// Output: "helo wrd" +``` + +## How it works +- Create an empty array called unique +- Loop through each character or element in the input +- Check if the current value already exists in unique +- If it does not exist, we add it to unique +- After the loop: +- If the input is a string, we convert the array into a string +- Remove commas using replace +- Finally, return the result + +## References +- [MDN – Array.prototype.includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) +- [MDN – typeof operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) + + +## Problem Added By +- [GitHub](https://github.com/rimsha-shoukat) + +## Contributing +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. + +Please make sure to update tests as appropriate. diff --git a/L-B/0027 Remove duplicates/removeDuplicates.js b/L-B/0027 Remove duplicates/removeDuplicates.js new file mode 100644 index 0000000..29f6dba --- /dev/null +++ b/L-B/0027 Remove duplicates/removeDuplicates.js @@ -0,0 +1,18 @@ +function removeDuplicates(input) { + let unique = []; + for (let i = 0; i < input.length; i++) { + if (!unique.includes(input[i])) { + unique.push(input[i]); + } + } + if(typeof(input) === "string"){ + unique = unique.toString().replace(/,/g, ''); + } + console.log(unique); + return unique; +} + +removeDuplicates([2, 4, 8, 4, 2, 6, 9, 2, 6, 8, 10]); +removeDuplicates([1, 1, 1, 4, 0, 6, -2, 2, 6, 7, 10]); +removeDuplicates("zoom"); +removeDuplicates("hello world"); \ No newline at end of file diff --git a/L-I/0012 Length converter/README.md b/L-I/0012 Length converter/README.md new file mode 100644 index 0000000..82560ba --- /dev/null +++ b/L-I/0012 Length converter/README.md @@ -0,0 +1,79 @@ +# 0012 lengthConverter ( L-I ) + +## Problem +Create a function that converts a given length value from one unit to another. +The function should: +- Accept a numeric length +- Accept a fromUnit and toUnit +- Support common metric and imperial units +- Return the converted value rounded to 5 decimal places +- Throw an error if an invalid unit is provided + +## Supported Units +- meters +- kilometers +- centimeters +- millimeters +- inches +- feet +- yards +- miles + +## Solution +```js +function lengthConverter(length, fromUnit, toUnit) { + const units = { + "meters": 1, + "kilometers": 1000, + "centimeters": 0.01, + "millimeters": 0.001, + "inches": 0.0254, + "feet": 0.3048, + "yards": 0.9144, + "miles": 1609.34 + }; + + if (!units[fromUnit] || !units[toUnit]) { + throw new Error("Invalid unit"); + } + const meters = length * units[fromUnit]; + console.log((meters / units[toUnit]).toFixed(3)); + return (meters / units[toUnit]).toFixed(3); +} +``` + +## Test Cases +```js +removeDuplicates([2, 4, 8, 4, 2, 6, 9, 2, 6, 8, 10]); +// Output: [2, 4, 8, 6, 9, 10] + +removeDuplicates([1, 1, 1, 4, 0, 6, -2, 2, 6, 7, 10]); +// Output: [1, 4, 0, 6, -2, 2, 7, 10] + +removeDuplicates("zoom"); +// Output: "zom" + +removeDuplicates("hello world"); +// Output: "helo wrd" +``` + +## How it works +- Define a units object where each unit is mapped to its value in meters +- Validate both fromUnit and toUnit +- The input length is first converted into meters +- Then we convert meters into the target unit +- The result is rounded to 5 decimal places using toFixed(5) +- Finally, the converted value is returned + +## References +- [Wikipedia – Unit conversion](https://en.wikipedia.org/wiki/Unit_conversion) +- [MDN – JavaScript Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) + + +## Problem Added By +- [GitHub](https://github.com/rimsha-shoukat) + +## Contributing +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. + +Please make sure to update tests as appropriate. \ No newline at end of file diff --git a/L-I/0012 Length converter/lengthConverter.js b/L-I/0012 Length converter/lengthConverter.js new file mode 100644 index 0000000..d6a78f1 --- /dev/null +++ b/L-I/0012 Length converter/lengthConverter.js @@ -0,0 +1,29 @@ +function lengthConverter(length, fromUnit, toUnit) { + const units = { + "meters": 1, + "kilometers": 1000, + "centimeters": 0.01, + "millimeters": 0.001, + "inches": 0.0254, + "feet": 0.3048, + "yards": 0.9144, + "miles": 1609.34 + }; + + if (!units[fromUnit] || !units[toUnit]) { + throw new Error("Invalid unit"); + } + + const meters = length * units[fromUnit]; + console.log((meters / units[toUnit]).toFixed(2)); + return (meters / units[toUnit]).toFixed(2); +} + +lengthConverter(100, "meters", "kilometers"); +lengthConverter(5, "miles", "feet"); +lengthConverter(12, "inches", "centimeters"); +lengthConverter(2500, "centimeters", "meters"); +lengthConverter(3, "kilometers", "miles"); +lengthConverter(10, "feet", "yards"); +lengthConverter(5000, "millimeters", "meters"); +lengthConverter(2, "yards", "inches"); \ No newline at end of file