We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importReact,{useState}from"react";importTablefrom"./Table";functionvalidateConcurrencyInput(valueStr,balanceValue){if(valueStr==="")return'Amountcannotbeempty';constvalueNumeric=parseFloat(valueStr);if(valueNumeric<0.01)return'Amountcannotbelessthan$0.01';if(valueNumeric>balanceValue)return'Amountcannotexceedtheavailablebalance';return"";}functionMain(){const_balanceValue=17042.67;const[value,setValue]=useState("");const[errorText,setErrorText]=useState("");functiononInputChange(e){constinputValue=e.target.value;consterrorText=validateConcurrencyInput(inputValue,_balanceValue);setErrorText(errorText);setValue(inputValue);}return(<divclassName="layout-column align-items-center mx-auto"><h1>CryptoRankExchange</h1><section><divclassName="card-text layout-column align-items-center mt-12 px-8 flex text-center"><label>Iwanttoexchange$<inputclassName="w-10"data-testid="amount-input"requiredtype="number"placeholder="USD"value={value}onChange={onInputChange}min={0}step="0.01"/>{" "}ofmy$<span>{_balanceValue}</span>:</label>{errorText&&(<pdata-testid="error"className="form-hint error-text mt-3 pl-0 ml-0">{errorText}</p>)}{/* The errors can be Amount cannot be empty /be less than $0.01/exceed the available balance */}</div></section><Tablevalue={value}hasError={errorText!==""}/></div>);}exportdefaultMain;-------------------------------------------------------------importReactfrom"react";import{cryptocurrencyList}from"../cryptocurrency-list";functionformatNumberOfCoins(value){returnvalue.toFixed(8);}functionTable({value,hasError}){constvalueNumeric=parseFloat(value);constconcurrencyValue=isNaN(valueNumeric)?0:valueNumeric;constshowConcurrencyValue=!hasError;return(<divclassName="card card-text mt-10 mx-4"><tableclassName="mb-0"><thead><tr><th>Cryptocurrency</th><th>ExchangeRate</th><th>NumberofCoins</th></tr></thead><tbodydata-testid="exchange-data">{cryptocurrencyList.map((concurrencyItem=>(<trkey={concurrencyItem.code}><td>{concurrencyItem.name}</td><td>1USD={concurrencyItem.rate}{concurrencyItem.code}</td><td>{showConcurrencyValue?formatNumberOfCoins(concurrencyValue*concurrencyItem.rate):'n/a'}</td></tr>)))}</tbody></table></div>);}exportdefaultTable;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
CryptoRank Exchange
You are viewing a single comment's thread. Return to all comments →