From 8813c4bfdb2ae51e27332ac8673e1ee8fdf7dd48 Mon Sep 17 00:00:00 2001 From: "colin-codegen[bot]" <137733214+colin-codegen[bot]@users.noreply.github.com> Date: Fri, 18 Aug 2023 21:03:33 +0000 Subject: [PATCH 1/2] Implemented recursive function in Chat.tsx --- src/components/Chat/Chat.tsx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/components/Chat/Chat.tsx b/src/components/Chat/Chat.tsx index 3898051..8067c2f 100644 --- a/src/components/Chat/Chat.tsx +++ b/src/components/Chat/Chat.tsx @@ -41,7 +41,16 @@ const createMessage = (text: string, isUserMessage: boolean): Message => { }; }; +// Implement the recursive function that sums an array of integers +const recursiveArraySum = (arr: number[]): number => { + if (arr.length === 0) { + return 0; + } + return arr[0] + recursiveArraySum(arr.slice(1)); +}; + export default function Chat() { + const userId = useAtomValue(chatUserIdAtom); // ref to track text area and scroll text into view From c33230dc20a8994a27b7327ffac2b8041e4a447a Mon Sep 17 00:00:00 2001 From: "colin-codegen[bot]" <137733214+colin-codegen[bot]@users.noreply.github.com> Date: Fri, 18 Aug 2023 21:06:53 +0000 Subject: [PATCH 2/2] Moved recursive function implementation inside useEffect hook as per feedback --- src/components/Chat/Chat.tsx | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/components/Chat/Chat.tsx b/src/components/Chat/Chat.tsx index 8067c2f..0ed8143 100644 --- a/src/components/Chat/Chat.tsx +++ b/src/components/Chat/Chat.tsx @@ -41,18 +41,23 @@ const createMessage = (text: string, isUserMessage: boolean): Message => { }; }; -// Implement the recursive function that sums an array of integers -const recursiveArraySum = (arr: number[]): number => { - if (arr.length === 0) { - return 0; - } - return arr[0] + recursiveArraySum(arr.slice(1)); -}; + export default function Chat() { + useEffect(() => { + // Implement the recursive function that sums an array of integers + const recursiveArraySum = (arr: number[]): number => { + if (arr.length === 0) { + return 0; + } + return arr[0] + recursiveArraySum(arr.slice(1)); + }; + }, []); + const userId = useAtomValue(chatUserIdAtom); + // ref to track text area and scroll text into view const ref = useRef(null);