diff --git a/Converter All by Manas/app.js b/Converter All by Manas/app.js new file mode 100644 index 0000000..038bded --- /dev/null +++ b/Converter All by Manas/app.js @@ -0,0 +1,224 @@ +const BASE_URL= "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies"; //We have removed eur/jpy.json this part to use in line no 47. +const dropdowns = document.querySelectorAll(".dropdown select"); +const fromCurr = document.querySelector(".from select"); +const toCurr = document.querySelector(".to select"); +const msg = document.querySelector(".msg"); +// for(code in countryList){ +// console.log(code,countryList[code]); +// } we can access the country code and currency code here + +for(let select of dropdowns){ + for(currCode in countryList){ + let newOption = document.createElement("option"); + newOption.innerText = currCode; + newOption.value = currCode; + if (select.id === "from" && currCode === "INR"){ + newOption.selected = "selected"; + } else if (select.id === "to" && currCode === "USD"){ + newOption.selected = "selected"; // Here you can use any word. + } + select.append(newOption); + } + + select.addEventListener("change",(evt) => { + updateFlag(evt.target); + }); +} + +const updateFlag = (element) => { + let currCode = element.value; //Here you can use any word in place of currCode. + let countryCode = countryList[currCode]; // here [currCode] come from for in loop in line no 9. Here "" this value of code.js will come from this code. + let newSrc = `https://flagsapi.com/${countryCode}/flat/64.png` + let img = element.parentElement.querySelector("img"); // Here element is our select and we have to add image to the parent div select-container so element.parentElement is used. + img.src = newSrc; +}; + +const btn = document.querySelector("form button"); + +btn.addEventListener("click",(evt) => { + evt.preventDefault(); //It blocks the refresh and submit default behavior of the button + updateExchRate(); +}); + +const updateExchRate = async()=>{ + let amount = document.querySelector(".amount input"); + let amtVal = amount.value; + if (amtVal ==="" || amtVal < 1) { + amtVal = 1; + amount.value = "1"; + } + // console.log(fromCurr.value, toCurr.value); here values are in capital but our api will work in lowercase so bellow we have to covert it to lower case + const URL = `${BASE_URL}/${fromCurr.value.toLowerCase()}/${toCurr.value.toLowerCase()}.json`; // fromCurr and toCurr is defined in line No. 3 and 4 + let response = await fetch(URL); + console.log(response); + let data = await response.json() + console.log(data); + let rate = await data[toCurr.value.toLowerCase()]; + console.log(rate); + + let finalAmount = amtVal * rate; + msg.innerText = `${amtVal} ${fromCurr.value} = ${finalAmount} ${toCurr.value}`; +} + +window.addEventListener("load",() => { + updateExchRate(); +}) + // to show update value in
tag when opening refer to video last part. + + + +function calculateAge(){ + const dobInput = document.getElementById("doB1").value; + const toDateInput = document.getElementById("ToDate1").value; + const doB = new Date(dobInput); + const toDate = new Date(toDateInput); + if(!dobInput || !toDateInput){ + alert("Please Enter Valid Date !!"); + return; + } + const resultElement = document.getElementById("ResultDisplay"); + const ageInMilliSeconds = Math.abs(toDate - doB); + const ageInDays = Math.ceil(ageInMilliSeconds/(1000*60*60*24)); + + const Years = Math.floor(ageInDays/365); + const remainingDays = ageInDays % 365; + const Months = Math.floor(remainingDays/30); + const Days = Math.floor(remainingDays % 30); + + resultElement.innerText = `Your Age is ${Years} Years, ${Months} Months & ${Days} days`; + +} +const btn2 = document.getElementById("ageCal"); + +btn2.addEventListener("click",() => { + calculateAge(); +}); + +//Length Converter + +function validateLengthConverterForm(){ + _cmnRemoveAllErrorMessage(); + let fromLength = document.getElementById("fromLength").value; + if(fromLength = "" || isNaN(fromLength) || (! isNaN(fromLength) && Number(fromLength)) <= 0){ + _cmnShowAllErrorMessage("fromLength","Enter valid length"); + return false; + } + else{ + return true; + } +} +function resetLengthConverter(){ + if(confirm("Are you sure to reset?")){ + document.getElementById("fromLength").value = ""; + document.getElementById("fromUnit").value = "Millimeter"; + document.getElementById("toUnit").value = "Centimeter" + document.getElementById("toLength").value = ""; + _cmnRemoveAllErrorMessage(); + + _cmnHideElement("OutputResult"); + _cmnShowElement("OutputInfo", "flex"); + } +} + +function convertLength(fromLength, fromUnit, toUnit){ + fromLength = Number(fromLength); + let result = 0; + makeThisToMillimeter = 0; + inMillimeter = 0; + + //Converting all values to millimeter + + switch(fromUnit){ + case "Millimeter": + makeThisToMillimeter = 1; + break; + case "Centimeter": + makeThisToMillimeter = 10; + break; + case "Decimeter": + makeThisToMillimeter = 100; + break; + case "Meter": + makeThisToMillimeter = 1000; + break; + case "Kilometer": + makeThisToMillimeter = 1000000; + break; + case "Foot": + makeThisToMillimeter = 304.8; + break; + case "Inch": + makeThisToMillimeter = 25.4; + break; + case "Mile": + makeThisToMillimeter = 1609344; + break; + case "Yard": + makeThisToMillimeter = 914.4; + break; + } + + inMillimeter = fromLength * makeThisToMillimeter; + + switch(toUnit){ + case "Millimeter": + result = inMillimeter; + break; + case "Centimeter": + result = inMillimeter / 10; + break; + case "Decimeter": + result = inMillimeter / 100; + break; + case "Meter": + result = inMillimeter / 1000; + break; + case "Kilometer": + result = inMillimeter / 1000000; + break; + case "Foot": + result = inMillimeter / 304.8; + break; + case "Inch": + result = inMillimeter / 25.4; + break; + case "Mile": + result = inMillimeter / 1609344; + break; + case "Yard": + result = inMillimeter / 914.4; + break; + } + return result; +} +function calculateLength(){ + if(validateLengthConverterForm()){ + let fromUnit = document.getElementById("fromUnit").value; + let toUnit = document.getElementById("toUnit").value; + let fromLength = document.getElementById("fromLength").value; + let toLength = document.getElementById("toLength"); + + let finalValue = convertLength(fromLength,fromUnit,toUnit); + + toLength.value = Number(finalValue).toFixed(2); + + let showResult = document.querySelector("#lengthResult"); + showResult.innerHTML = `${fromLength} ${fromUnit} = ${Number(finalValue).toFixed(2)} ${toUnit}`; + + // Hide info and show result + + _cmnHideElement("OutputInfo"); + _cmnShowElement("OutputResult","flex"); + } +} + +let resetBtn = document.querySelector(".tool-btn-reset"); + +let calBtn = document.querySelector(".tool-btn-calculate"); + +resetBtn.onclick = () =>{ + resetLengthConverter(); +}; +calBtn.onclick = () =>{ + calculateLength(); +}; diff --git a/Converter All by Manas/common.js b/Converter All by Manas/common.js new file mode 100644 index 0000000..5935369 --- /dev/null +++ b/Converter All by Manas/common.js @@ -0,0 +1,211 @@ +const countryList = { + AED: "AE", + AFN: "AF", + XCD: "AG", + ALL: "AL", + AMD: "AM", + ANG: "AN", + AOA: "AO", + AQD: "AQ", + ARS: "AR", + AUD: "AU", + AZN: "AZ", + BAM: "BA", + BBD: "BB", + BDT: "BD", + XOF: "BE", + BGN: "BG", + BHD: "BH", + BIF: "BI", + BMD: "BM", + BND: "BN", + BOB: "BO", + BRL: "BR", + BSD: "BS", + NOK: "BV", + BWP: "BW", + BYR: "BY", + BZD: "BZ", + CAD: "CA", + CDF: "CD", + XAF: "CF", + CHF: "CH", + CLP: "CL", + CNY: "CN", + COP: "CO", + CRC: "CR", + CUP: "CU", + CVE: "CV", + CYP: "CY", + CZK: "CZ", + DJF: "DJ", + DKK: "DK", + DOP: "DO", + DZD: "DZ", + ECS: "EC", + EEK: "EE", + EGP: "EG", + ETB: "ET", + EUR: "FR", + FJD: "FJ", + FKP: "FK", + GBP: "GB", + GEL: "GE", + GGP: "GG", + GHS: "GH", + GIP: "GI", + GMD: "GM", + GNF: "GN", + GTQ: "GT", + GYD: "GY", + HKD: "HK", + HNL: "HN", + HRK: "HR", + HTG: "HT", + HUF: "HU", + IDR: "ID", + ILS: "IL", + INR: "IN", + IQD: "IQ", + IRR: "IR", + ISK: "IS", + JMD: "JM", + JOD: "JO", + JPY: "JP", + KES: "KE", + KGS: "KG", + KHR: "KH", + KMF: "KM", + KPW: "KP", + KRW: "KR", + KWD: "KW", + KYD: "KY", + KZT: "KZ", + LAK: "LA", + LBP: "LB", + LKR: "LK", + LRD: "LR", + LSL: "LS", + LTL: "LT", + LVL: "LV", + LYD: "LY", + MAD: "MA", + MDL: "MD", + MGA: "MG", + MKD: "MK", + MMK: "MM", + MNT: "MN", + MOP: "MO", + MRO: "MR", + MTL: "MT", + MUR: "MU", + MVR: "MV", + MWK: "MW", + MXN: "MX", + MYR: "MY", + MZN: "MZ", + NAD: "NA", + XPF: "NC", + NGN: "NG", + NIO: "NI", + NPR: "NP", + NZD: "NZ", + OMR: "OM", + PAB: "PA", + PEN: "PE", + PGK: "PG", + PHP: "PH", + PKR: "PK", + PLN: "PL", + PYG: "PY", + QAR: "QA", + RON: "RO", + RSD: "RS", + RUB: "RU", + RWF: "RW", + SAR: "SA", + SBD: "SB", + SCR: "SC", + SDG: "SD", + SEK: "SE", + SGD: "SG", + SKK: "SK", + SLL: "SL", + SOS: "SO", + SRD: "SR", + STD: "ST", + SVC: "SV", + SYP: "SY", + SZL: "SZ", + THB: "TH", + TJS: "TJ", + TMT: "TM", + TND: "TN", + TOP: "TO", + TRY: "TR", + TTD: "TT", + TWD: "TW", + TZS: "TZ", + UAH: "UA", + UGX: "UG", + USD: "US", + UYU: "UY", + UZS: "UZ", + VEF: "VE", + VND: "VN", + VUV: "VU", + YER: "YE", + ZAR: "ZA", + ZMK: "ZM", + ZWD: "ZW", +}; +function _cmnRemoveAllErrorMessage() +{ + let allErrorBorder = document.getElementsByClassName('.tool-error-border'); + console.log(allErrorBorder.length) + let allErrorMessage = document.getElementsByClassName('tool-error-message'); + let i; + // remove border + for(i = (allErrorBorder.length) - 1; i>=0; i--) + { + allErrorBorder[i].classList.remove("tool-error-border"); + } + // remove error message + for(i = (allErrorMessage.length) - 1; i>=0; i--) + { + allErrorMessage[i].remove(); + } +} + +function _cmnShowAllErrorMessage(fieldID,errorMessage){ + let inputField = document.getElementById(fieldID); + inputField.classList.add("tool-error-border"); + inputField.focus(); + + let errorMessageElement = document.createElement("p"); + errorMessageElement.innerText = errorMessage; + errorMessageElement.classList.add("tool-error-message"); + inputField.parentNode.insertBefore(errorMessageElement, inputField.nextSibling); +} +function _cmnHideElement(elementID){ + let selectedDisplayValue = document.getElementById(elementID).style.display; + if(selectedDisplayValue != "none"){ + document.getElementById(elementID).style.display = "none"; + } +} + +function _cmnShowElement(elementID,displayName){ + let selectedDisplayValue = document.getElementById(elementID).style.display; + if(selectedDisplayValue != displayName){ + document.getElementById(elementID).style.display = displayName; + } +} +function updateLayOut(){ + _cmnHideElement("Age"); + _cmnHideElement("Length"); + _cmnHideElement("Currency"); + const selectedTool = document.getElementById("Tool-Select").value; + console.log(selectedTool) + _cmnShowElement(selectedTool,"block"); +} + \ No newline at end of file diff --git a/Converter All by Manas/index.html b/Converter All by Manas/index.html new file mode 100644 index 0000000..f89506d --- /dev/null +++ b/Converter All by Manas/index.html @@ -0,0 +1,150 @@ + + + + + +