Hack #81700
Unanswered
borarishav12
asked this question in
App Router
Hack
#81700
Replies: 1 comment
-
Yes |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
import React, { useState } from "react"; import { Card, CardContent } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Bar } from "react-chartjs-2";
const ColorPredictor = () => { const [history, setHistory] = useState([]); const [input, setInput] = useState(""); const [prediction, setPrediction] = useState(null);
const handleAdd = () => { const val = input.trim().toLowerCase(); if (["red", "green", "violet"].includes(val)) { setHistory([...history, val]); setInput(""); } };
const predictNext = () => { const n = 2; if (history.length < n) { setPrediction("Not enough data"); return; } const pattern = history.slice(-n).join(","); const map = {};
for (let i = 0; i < history.length - n; i++) {
const key = history.slice(i, i + n).join(",");
const next = history[i + n];
if (!map[key]) map[key] = {};
map[key][next] = (map[key][next] || 0) + 1;
}
const options = map[pattern];
if (!options) {
setPrediction("Pattern not found");
} else {
const sorted = Object.entries(options).sort((a, b) => b[1] - a[1]);
setPrediction(
Next likely color: ${sorted[0][0]}
);}
};
return (
Color Pattern Predictor
export default ColorPredictor;
Beta Was this translation helpful? Give feedback.
All reactions