JSFiddle - React, Tailwind, and code Playground
HTML
<div class="header">
<h1>Time is Money</h1>
<p> Find out how much time it will take you to buy something!</p>
</div>
<div class="test">$12</div>
<ul>
<li>$12</li>
<li>$14.99</li>
<li>$2</li>
<li>$dollars</li>
<li>14.99</li>
<li>$12,000</li>
</ul>
CSS
body {
font-family: Helvetica, Arial, sans-serif;
}
h1{
font-size: 30px;
color: green;
}
p {
font-size: 18px;
}
JavaScript
/*
Code by Jonathan Shariat - Feb 16, 2013
...Unless it's bad, then it's not by Jonathan Shariat...
\$[^\()!@#%^&*]\d*.?\d{1,2} // regex for selecting prices
*/
var time = 60; //increments of time is always 60
//Takes a price and converts it into time using their salary
var priceToTime = function(price,dph){
var hoursToBuy = price/dph;
var totalTime = {}; //object
//Now need to calculate years, months, days, hours, minutes
// Plan: If greater than 1x, divide and minus that from total and move to the next one.
//Years
if (hoursToBuy >= 8765.81){
var years = Math.floor(hoursToBuy/8765.81);
hoursToBuy = hoursToBuy - (years * 8765.81);
totalTime.years = years+" years";
}
//Months
if (hoursToBuy >= 730.484){
var months = Math.floor(hoursToBuy/730.484);
hoursToBuy = hoursToBuy - (months * 730.484);
totalTime.months = months+" months";
}
//Weeks
if (hoursToBuy >= 168){
var weeks = Math.floor(hoursToBuy/168);
hoursToBuy = hoursToBuy - (weeks * 168);
totalTime.weeks = weeks+" weeks";
}
//Days
if (hoursToBuy >= 24){
var days = Math.floor(hoursToBuy/24);
hoursToBuy = hoursToBuy - (days * 24);
totalTime.days = days+" days";
}
var hours = Math.floor(hoursToBuy);
if (hours > 0){
totalTime.hours = hours+" hours";
};
var minutes = ((hoursToBuy - Math.floor(hoursToBuy)).toFixed(1))*10;
if (minutes > 0){
totalTime.minutes = minutes+" minutes";
};
return totalTime;
}
$(document).ready(function(){
var regExp = new RegExp("\$[^\()!@#%^&*]\d*.?\d{1,2}"); // regex pattern string
var pageText = document.body.innerHTML;
document.body.innerHTML = pageText.replace(/12/g, 'IT WORKS!!!!!!!!!!!');
$('button').click(function(){
$('#result1').remove();
var userPrice = $('input[name=price]').val();
var userDph = $('input[name=dph]').val();
var result = priceToTime(userPrice,userDph);
function printResult(totalObj) {
var result = "";
for (var i in totalObj) {
if...