Help with javascript

I’ve been working on quite a long and poorly organized program for a little while now, and I’ve run into a problem I haven’t been able to find a solution for this.

The relevant code snippets are (I think):

state = {
  letters: [],
  writing: Number
  writing_content: {}
}

const characters = ["bunch","of","characters"]

function finalLetter() {
	let letter = {
		recipient: characters[state.writing],
	}
	letter = Object.assign(letter, deepCopy(state.writing_content))
	return letter
}

function deepCopy(object) {
	return JSON.parse(JSON.stringify(object))
}

function showTextNode(TextNodeID) {
  previousTextNode = currentTextNode
  currentTextNode = textNodeIndex
  const textNode = textNodes.find(textNode => textNode.id === textNodeIndex)
  while (textNode.title.includes("undefined")){
    textNode.title = textNode.title.replace("undefined", eval(textNode.updateTitle[i])).replace("_", " ")
	  i++
  }
textNode.options.forEach(option => {
		if (showOption(option)) {

			const button = document.createElement('button')
			button.innerText = option.text

			if (option.leave) option.nextText = outsideNode

			button.classList.add('btn')
			button.addEventListener('click', () => selectOption(option))

			optionButtonsElement.appendChild(button)
		}
	})
}

function showOption(option) {
	return option.requiredState == null || option.requiredState(state)
}

function selectOption(option) {
	const nextTextNodeId = option.nextText
	if (Object.hasOwn(option, "setState")) {
		for (i=0; i<option.setState.category.length;i++) {
		if (option.setState.category[i] != "general") state[option.setState.category[i]] = Object.assign(state[option.setState.category[i]], option.setState.changes[i])
		else state = Object.assign(state, option.setState.changes[i])
		}
	}
	if (option.send) sendLetter(state.current_letter)
	showTextNode(nextTextNodeId)
}

let textNodes = [
  {
    		id: 99919,
		title: `Letter to ${characters[state.writing]}`,
		updateTitle: ["characters[state.writing]"],
		text: "Do you wish to send it immediately?",
		options: [
			{
				text: "No",
				setState: {
					category:["general"], changes:{letters: state.letters.concat(finalLetter()),
				}},
				leave: true,
				nextText: outsideNode
			}
		]
	}
]

(This is borderline unreadable in the editor. I hope it’ll look better in the post)

The problem is quite simple; After I click the “No” button, it does everything it’s supposed to, but state.letters remains empty. I’ve checked that the whole “setState” stuff works with other options, and finalLetter() works as well when it’s called on its own.

I’ve tried moving the function call into selectOption(option), but that didn’t help.

I must admit that I have been learning JavaScript on the go, as I started with a tutorial specific for basic text adventures and tried to hammer my own system into it without really knowing how the language works, and it has caused me grief in the past, but I’ve never been so clueless as I am now.

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

Here’s what I have now (the entire code, as the parts in the post were hastily copied over because the whole thing is a mess): https://codepen.io/aaaaaaaaaaaaaaaaaa1/pen/myrxepR

And what I’ve tried: https://codepen.io/aaaaaaaaaaaaaaaaaa1/pen/PwGRPJj

(I didn’t really expect this to work, but I couldn’t think of anything else)

It’s horrible to read. As specified in the post, I’ve been learning on the fly, and I’m also on a deadline (don’t worry, it’s voluntary), so I haven’t really kept up with best (or even just "good”) practices, resulting in this confusing mess.

If there’s anything fundamental I’m missing, please do let me know.