From 5f61cf50bea370bc9e8b313aea8018ecdb16ef51 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Wed, 19 Apr 2023 19:57:22 +0200 Subject: [PATCH] add cpuRepository class --- js/cpuRepository.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 js/cpuRepository.js diff --git a/js/cpuRepository.js b/js/cpuRepository.js new file mode 100644 index 0000000..ad84a99 --- /dev/null +++ b/js/cpuRepository.js @@ -0,0 +1,35 @@ +class cpuRepository { + #cpuList; + + #currentCPU; + #nextCPU; + + async init() { + const fetchResult = await fetch("./js/data.json"); + this.#cpuList = await fetchResult.json(); + + this.#currentCPU = this.getRandomCpu(); + this.#nextCPU = this.getRandomCpu(); + } + + get currentCpu() { + return this.#currentCpu; + } + + get nextCpu() { + return this.#nextCpu; + } + + getRandomCpu() { + let randomIndex = this.getRandomInt(0, this.#cpuList.length); + this.#cpuList[randomIndex]["name"] = this.#cpuList[randomIndex]["name"].split('@')[0]; + return this.#cpuList[randomIndex]; + } + + getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive + } + +} \ No newline at end of file