How find out the rate of ETH in USD in the contract?
To automatically get this data, rather than manually feeding it in by calling a function in the contract, you would need to use an oracle to get the real-world information into your contract. You could use:
Check out this blog post for a more thorough explanation and example of how to fetch ETH/USD price data using Chainlink Price Feeds
First interpretation: This is can solidty?
Yes, that code is Solidity, which is the language smart contracts are written in. You can't (currently) use native Javascript as a smart contract language. (There's no compiler that creates EVM bytecode from Javascript.)
Second interpretation: This can be done in Solidity?
Yes, see the threads I link to. Note that using an oracle isn't free. You will need to pay for the service.