Build a Storytelling App - Step 14

Tell us what’s happening:

This part code is not working of the story property assign to array

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Storytelling App</title>
    <link rel="stylesheet" href="./styles.css" />
</head>

<body>
    <h1>Want to hear a short story?</h1>
    <main class="story-container">
            <div class="btn-container">
                <button class="btn" id="scary-btn">Scary Story</button>
                <button class="btn" id="funny-btn">Funny Story</button>
                <button class="btn" id="adventure-btn">Adventure Story</button>
            </div>
            <p id="result"></p>
    </main>
    <script src="./script.js"></script>
</body>
</html>
/* file: script.js */
let storyContainer = document.querySelector(".story-container");

const scaryStoryBtn = document.getElementById("scary-btn");
const funnyStoryBtn = document.getElementById("funny-btn");
const adventureStoryBtn = document.getElementById("adventure-btn");

const resultParagraph = document.getElementById("result");

const storyObj = {
  scary: {
    story: `In the dark woods, a group of friends stumbled upon an old, abandoned cabin. They enter the cabin and awaken something malevolent that had been dormant for centuries.`,
    borderColor: "#ee4b2b",
  },
  funny: {
    story: `During a camping trip, Mark decided to show off his culinary skills by cooking dinner over an open fire. However, his attempt caused him to burn the dinner as well as his eyebrows off.`,
    borderColor: "#f1be32",
  },
  adventure: {
    story: `Lost in the heart of the Amazon rain forest, Sarah and Jake stumbled upon an ancient temple. They braved deadly traps and encountered strange wildlife, all while deciphering cryptic clues left behind by a mysterious civilization.`,
    borderColor: "#acd157"
  },
};


// User Editable Region

function displayStory(genre) {
  const result = document.getElementById("result");

  if (storyObj.hasOwnProperty(genre)) {
    result.textContent = storyObj[genre].story;
    storyContainer.style.borderColor = storyObj[genre].borderColor;
  } else {
    result.textContent = "Sorry, that genre is not available.";
    storyContainer.style.borderColor = "#000"; // default or error color
  }
}


// User Editable Region


scaryStoryBtn.addEventListener("click", displayStory);


/* file: styles.css */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --dark-grey: #1b1b32;
    --black: #000;
    --white: #fff;
    --golden-yellow: #fecc4c;
    --yellow: #ffcc4c;
    --gold: #feac32;
    --orange: #ffac33;
    --dark-orange: #f89808;
}

body {
    background-color: var(--dark-grey);
    color: var(--white);
}

h1,
#result {
    text-align: center;
}

h1 {
    margin: 10px 0 15px;
}

.story-container {
    margin: auto;
    padding: 10px;
    width: 80%;
    border-style: double;
    border-width: 14px;
    border-color: var(--white);
}

.btn-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

@media (min-width: 760px) {
    .btn-container {
        flex-direction: row;
    }
}

#result {
    margin-top: 15px;
    font-size: 1.2rem;
    line-height: 30px;
}

.btn {
    cursor: pointer;
    width: 200px;
    margin: 10px 0 10px 0.5rem;
    color: var(--black);
    background-color: var(--gold);
    background-image: linear-gradient(var(--golden-yellow), var(--orange));
    border-color: var(--gold);
    border-width: 3px;
}

.btn:hover {
    background-image: linear-gradient(var(--yellow), var(--dark-orange));
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

Build a Storytelling App - Step 14

// User Editable Region

function displayStory(genre) {
const result = document.getElementById(“result”);

if (storyObj.hasOwnProperty(genre)) {
result.textContent = storyObj[genre].story;
storyContainer.style.borderColor = storyObj[genre].borderColor;
} else {
result.textContent = “Sorry, that genre is not available.”;
storyContainer.style.borderColor = “#000”; // default or error color
}
}this section

Hi @ishansainju1

You should call the displayStory function with the "scary" genre.

You need to call the function with an argument.

Happy coding

function displayStory(genre) {
const storyContainer = document.getElementById(‘storyContainer’);
const resultPara = document.getElementById(‘result’);

if (storyObj.hasOwnProperty(genre)) {
    resultPara.textContent = storyObj[genre].story;
    storyContainer.style.borderColor = storyObj[genre].borderColor;
}

}
displayStory(“scary”); ;like this but there shows the
error

The function call should be in the global scope.

Try placing it below all the other code.

Tell us what’s happening:

let result = document.querySelector(“#result”);
function displayStory(genre) {
if(storyObj[genre]) {
result.textContent = storyObj[genre].story;
result.style.borderColor = storyObj[genre].borderColor;
}
}
displayStory(“scary”);

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Storytelling App</title>
    <link rel="stylesheet" href="./styles.css" />
</head>

<body>
    <h1>Want to hear a short story?</h1>
    <main class="story-container">
            <div class="btn-container">
                <button class="btn" id="scary-btn">Scary Story</button>
                <button class="btn" id="funny-btn">Funny Story</button>
                <button class="btn" id="adventure-btn">Adventure Story</button>
            </div>
            <p id="result"></p>
    </main>
    <script src="./script.js"></script>
</body>
</html>
/* file: script.js */
let storyContainer = document.querySelector(".story-container");

const scaryStoryBtn = document.getElementById("scary-btn");
const funnyStoryBtn = document.getElementById("funny-btn");
const adventureStoryBtn = document.getElementById("adventure-btn");

const resultParagraph = document.getElementById("result");

const storyObj = {
  scary: {
    story: `In the dark woods, a group of friends stumbled upon an old, abandoned cabin. They enter the cabin and awaken something malevolent that had been dormant for centuries.`,
    borderColor: "#ee4b2b",
  },
  funny: {
    story: `During a camping trip, Mark decided to show off his culinary skills by cooking dinner over an open fire. However, his attempt caused him to burn the dinner as well as his eyebrows off.`,
    borderColor: "#f1be32",
  },
  adventure: {
    story: `Lost in the heart of the Amazon rain forest, Sarah and Jake stumbled upon an ancient temple. They braved deadly traps and encountered strange wildlife, all while deciphering cryptic clues left behind by a mysterious civilization.`,
    borderColor: "#acd157"
  },
};

// User Editable Region

let result = document.querySelector("#result");
function displayStory(genre) {
  if(storyObj[genre]) {
    result.textContent = storyObj[genre].story;
   result.style.borderColor = storyObj[genre].borderColor;
  }
}
displayStory("scary");

// User Editable Region

scaryStoryBtn.addEventListener("click", displayStory);


/* file: styles.css */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --dark-grey: #1b1b32;
    --black: #000;
    --white: #fff;
    --golden-yellow: #fecc4c;
    --yellow: #ffcc4c;
    --gold: #feac32;
    --orange: #ffac33;
    --dark-orange: #f89808;
}

body {
    background-color: var(--dark-grey);
    color: var(--white);
}

h1,
#result {
    text-align: center;
}

h1 {
    margin: 10px 0 15px;
}

.story-container {
    margin: auto;
    padding: 10px;
    width: 80%;
    border-style: double;
    border-width: 14px;
    border-color: var(--white);
}

.btn-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

@media (min-width: 760px) {
    .btn-container {
        flex-direction: row;
    }
}

#result {
    margin-top: 15px;
    font-size: 1.2rem;
    line-height: 30px;
}

.btn {
    cursor: pointer;
    width: 200px;
    margin: 10px 0 10px 0.5rem;
    color: var(--black);
    background-color: var(--gold);
    background-image: linear-gradient(var(--golden-yellow), var(--orange));
    border-color: var(--gold);
    border-width: 3px;
}

.btn:hover {
    background-image: linear-gradient(var(--yellow), var(--dark-orange));
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36

Challenge Information:

Build a Storytelling App - Step 14

Hi, do you have a question?

Please describe what’s happening,
if you need help with a test, what the test is?
Is there any error or feedback
Can you show the part of your code which fulfills the test requirement?
What have you tried to troubleshoot?

removed by moderator