Skip to content

Commit 6c2105e

Browse files
committed
pp
1 parent 3e7bc41 commit 6c2105e

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Oppsummering 2023.pptx

7.02 MB
Binary file not shown.

løsninger/f3/Task1.tsx

+44-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,47 @@ const Counter: React.FC = () => {
2222
);
2323
}
2424

25-
export default Counter;
25+
export default Counter;
26+
27+
function findFirst<T>(items: T[], predicate: (item: T) => boolean): T | undefined {
28+
for (const item of items) {
29+
if (predicate(item)) {
30+
return item;
31+
}
32+
}
33+
return undefined;
34+
}
35+
36+
findFirst
37+
38+
let someValue: unknown = "this is a string";
39+
let strLength: number = (someValue as string).length;
40+
41+
strLength
42+
43+
// Definerer en grensesnitt for den forventede responsstrukturen
44+
interface ApiResponse {
45+
userId: number;
46+
id: number;
47+
title: string;
48+
completed: boolean;
49+
}
50+
51+
// En funksjon som gjør et eksternt kall
52+
async function fetchTodo(id: number): Promise<ApiResponse> {
53+
const response = await fetch(`https://jsonplaceholder.typicode.com/todos/${id}`);
54+
55+
// Sjekker om responsen er vellykket
56+
if (!response.ok) {
57+
throw new Error(`Error! status: ${response.status}`);
58+
}
59+
60+
// Anta at responsen er i JSON-format
61+
const result: ApiResponse = await response.json();
62+
return result;
63+
}
64+
65+
// Bruker funksjonen
66+
fetchTodo(1)
67+
.then(todo => console.log('Fetched todo:', todo))
68+
.catch(error => console.error('An error occurred:', error));

0 commit comments

Comments
 (0)