-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·46 lines (39 loc) · 1.31 KB
/
script.js
File metadata and controls
executable file
·46 lines (39 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Practice: Pass values between functions
*
* - Create two functions
* - Main function creates article element with data from object
* - Helper function creates.
*/
// Object with data
const articleData = {
title: "JavaScript Practice",
content: "This is a practice exercise to pass values between functions.",
author: "Serhat Ece",
date: "2024-07-27",
};
// Main function to create the article element
function createArticle(data) {
// Create article element
const article = document.createElement("article");
// Use helper functions to create and append elements
const title = createElement("h1", data.title);
const content = createElement("p", data.content);
const author = createElement("p", `Author: ${data.author}`);
const date = createElement("p", `Date: ${data.date}`);
// Append elements to the article
article.appendChild(title);
article.appendChild(content);
article.appendChild(author);
article.appendChild(date);
// Append the article to the body or any other container
document.body.appendChild(article);
}
// Helper function to create an element with text content
function createElement(tag, text) {
const element = document.createElement(tag);
element.textContent = text;
return element;
}
// Call the main function with article data
createArticle(articleData);