File tree Expand file tree Collapse file tree 3 files changed +106
-0
lines changed
Source-Code/CurrencyConverter Expand file tree Collapse file tree 3 files changed +106
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < title > Currency Converter</ title >
7+ < link rel ="stylesheet " href ="style.css ">
8+ </ head >
9+ < body >
10+ < div class ="container ">
11+ < h1 > Currency Converter</ h1 >
12+ < div class ="converter ">
13+ < input type ="number " id ="amount " placeholder ="Amount " />
14+ < select id ="from-currency ">
15+ < option value ="USD "> USD</ option >
16+ < option value ="EUR "> EUR</ option >
17+ < option value ="INR "> INR</ option >
18+ < option value ="GBP "> GBP</ option >
19+ </ select >
20+ < span > to</ span >
21+ < select id ="to-currency ">
22+ < option value ="INR "> INR</ option >
23+ < option value ="USD "> USD</ option >
24+ < option value ="EUR "> EUR</ option >
25+ < option value ="GBP "> GBP</ option >
26+ </ select >
27+ < button id ="convert "> Convert</ button >
28+ </ div >
29+ < div id ="result "> </ div >
30+ </ div >
31+ < script src ="script.js "> </ script >
32+ </ body >
33+ </ html >
Original file line number Diff line number Diff line change 1+ document . getElementById ( 'convert' ) . addEventListener ( 'click' , ( ) => {
2+ const amount = document . getElementById ( 'amount' ) . value ;
3+ const fromCurrency = document . getElementById ( 'from-currency' ) . value ;
4+ const toCurrency = document . getElementById ( 'to-currency' ) . value ;
5+
6+ if ( amount === '' || Number . isNaN ( Number ( amount ) ) ) {
7+ alert ( 'Please enter a valid amount' ) ;
8+ return ;
9+ }
10+
11+ const apiUrl = `https://api.exchangerate-api.com/v4/latest/${ fromCurrency } ` ;
12+
13+ fetch ( apiUrl )
14+ . then ( ( response ) => response . json ( ) )
15+ . then ( ( data ) => {
16+ const exchangeRate = data . rates [ toCurrency ] ;
17+ const convertedAmount = ( amount * exchangeRate ) . toFixed ( 2 ) ;
18+ document . getElementById (
19+ 'result' ,
20+ ) . innerText = `${ amount } ${ fromCurrency } = ${ convertedAmount } ${ toCurrency } ` ;
21+ } )
22+ . catch ( ( error ) => {
23+ document . getElementById (
24+ 'result' ,
25+ ) . innerText = `Error fetching exchange rates. ${ error } Try again later.` ;
26+ } ) ;
27+ } ) ;
Original file line number Diff line number Diff line change 1+ body {
2+ font-family : Arial, sans-serif;
3+ background-color : # f4f4f4 ;
4+ margin : 0 ;
5+ padding : 0 ;
6+ display : flex;
7+ justify-content : center;
8+ align-items : center;
9+ height : 100vh ;
10+ }
11+
12+ .container {
13+ background-color : white;
14+ padding : 20px ;
15+ border-radius : 10px ;
16+ box-shadow : 0 4px 8px rgba (0 , 0 , 0 , 0.1 );
17+ width : 400px ;
18+ text-align : center;
19+ }
20+
21+ h1 {
22+ margin-bottom : 20px ;
23+ }
24+
25+ input , select , button {
26+ margin : 10px 0 ;
27+ padding : 10px ;
28+ font-size : 16px ;
29+ }
30+
31+ button {
32+ background-color : # 4CAF50 ;
33+ color : white;
34+ border : none;
35+ cursor : pointer;
36+ }
37+
38+ button : hover {
39+ background-color : # 45a049 ;
40+ }
41+
42+ # result {
43+ margin-top : 20px ;
44+ font-size : 18px ;
45+ font-weight : bold;
46+ }
You can’t perform that action at this time.
0 commit comments