Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: OOP Pricing #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h3 class="info-card__pageviews">
<span class="js-pageviews"></span>Pageviews
</h3>
<label class="info-card__slider" for="slider-input"></label>
<input type="range" step="4" min="8" max="36" id="slider-input">
<input type="range" step="1" min="0" max="4" id="slider-input">
<h3 class="info-card__price">
<span class="change-color">$</span><span class="js-input-value"></span><span class="js-discount-value"></span>/month
</h3>
Expand Down
32 changes: 13 additions & 19 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import "./scss/global.scss";

const pageviewCount = document.querySelector(".js-pageviews");
const pagesPerDollar = 6250;

const calculatePageviews = (dollars) => {
return dollars * pagesPerDollar;
const pagesAndPrices = {
views: ["10K", "50K", "100K", "500K", "1M"],
prices: [8, 12, 16, 24, 36]
};

const inputSlider = document.querySelector("input[type='range']");
let price = inputSlider.value;
pageviewCount.innerHTML = calculatePageviews(price);
// line 10 will set the max attr for the range input, that way if the pagesAndPrices object gets updated, the max gets updated automatically
inputSlider.setAttribute("max", ((pagesAndPrices).views).length - 1);
const pageviewCount = document.querySelector(".js-pageviews");
let price = pagesAndPrices.prices[inputSlider.value];
pageviewCount.innerHTML = pagesAndPrices.views[inputSlider.value];
const spanPriceDisplay = document.querySelector(".js-input-value");
spanPriceDisplay.innerHTML = price;

const discountedPriceDisplay = document.querySelector(".js-discount-value");
const discountToggle = document.querySelector("input[type='checkbox'");
const discount = 0.25;
discountToggle.checked = false;

const applyDiscount = (price) => {
return (price * (1 - discount));
const convertedPrice = pagesAndPrices.prices[price];
return (convertedPrice * (1 - discount));
};

const createNewPrice = (price) => {
Expand Down Expand Up @@ -51,18 +54,9 @@ discountToggle.addEventListener("input", function() {
}
});

const getInputValue = (inputValue) => {
return inputValue;
};

inputSlider.addEventListener("input", function(e) {
return (
spanPriceDisplay.innerHTML = getInputValue(e.target.value)
);
});

inputSlider.addEventListener("change", function(e) {
return (
pageviewCount.innerHTML = calculatePageviews(e.target.value)
spanPriceDisplay.innerHTML = pagesAndPrices.prices[e.target.value],
pageviewCount.innerHTML = pagesAndPrices.views[e.target.value]
);
});