What could be the reason my hex code generator is outputting variable names instead of their assigned values?

I am currently working on developing a unique hex code generator using random values. At the moment, my focus is on displaying six random values in the HTML.

    // The characters A-F and numbers 0-9 can be utilized
var button = document.querySelector(".hex-btn");
var color = document.querySelector(".chosen-color");

button.addEventListener("click", function () {
  var number = Math.floor(Math.random() * 10);
  var letterString = ["a", "b", "c", "d", "e", "f"];
  var chosenLetter = Math.floor(Math.random() * 6);
  var letter = letterString[chosenLetter];

  hex1 = hexOptions();
  hex2 = hexOptions();
  hex3 = hexOptions();
  hex4 = hexOptions();
  hex5 = hexOptions();
  hex6 = hexOptions();

  color.innerHTML = "#" + hex1 + hex2 + hex3 + hex4 + hex5 + hex6;
});

// Each hex value can be either a number or a letter 
function hexOptions() {
  var option = ["number", "letter"];
  var randomOption = Math.floor(Math.random() * 2);
  return option[randomOption];
}

I am experiencing an issue where it is showing variable names instead of their actual values. For instance, #numbernumberletternumberletternumber gets displayed. Any guidance on why this might be happening?

Answer №1

The hexOptions function is designed to output either 'number' or 'letter'. After that, the variables hex1 to hex6 are set based on this result, combined together, and then returned.

To achieve what you're aiming for, it would be better to utilize the outcome of the hexOptions function to select between a letter or a number. Here's an example:

button.addEventListener("click", function () {
  var number = Math.floor(Math.random() * 10);
  var letterString = ["a", "b", "c", "d", "e", "f"];
  var chosenLetter = Math.floor(Math.random() * 6);
  var letter = letterString[chosenLetter];

  hex1 = hexOptions();
  const hex1Value = hex1 === 'number' ? Math.random() * 10 : letter;
  // continue this process for each of your hex values

  color.innerHTML = "#" + hex1Value + hex2Value + hex3Value + hex4Value + hex5Value + hex6Value;
});

Answer №2

The query you raised earlier is directly linked to the outcome you're experiencing. I will provide an explanation on that.

let choices = ["apple", "banana"];

This array consists of 2 different fruits. It seems like you might have a different intention for using it.

By calling choices[0], you would receive the word "apple". This clarifies why you obtained that specific result.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Eliminating attributes in mapStateToProps

When wrapping material TextField with a redux component, it is important to consider that some properties should be used in mapStateToProps only and not passed directly to the component. Otherwise, an Unknown prop warning may occur. Even specifying an unde ...

Removing an item from an array in Mongoose

I'm encountering an issue with removing an Object from an Array and I'm unsure of the correct way to use $pull for this action. Any help would be greatly appreciated! JSON data "accountId": "62efd8c008f424af397b415d", "l ...

Updating databases with the click of a checkbox

Currently, I am developing a program for monitoring cars as part of my thesis. My current focus is on user management, and I have come across an issue where the database needs to be updated when the status of a checkbox changes. To visualize checkboxes, y ...

Challenge 14 on Project Euler: Array index dilemma discovered in an innovative approach

The specific issue can be found at I've devised what I believe to be an innovative solution. It doesn't rely on brute force. My approach is based on two key assumptions: 1) Minimizing the number of iterations through the sequence leads to faste ...

Tips for correctly formatting a string to use with the setValue() method

I am facing an issue with adding the value US punctuation + alphanumeric lowercase:[a-z0-9,.?;:!&()_'"]+ to a cell in my spreadsheet. The error message I receive is: Missing ) after argument list. I am unsure how to correct the string that inc ...

"Create a table from a multidimensional array and showcase it using jQuery: A step-by-step guide

I have successfully returned a multidimensional array from a PHP script to an HTML page via AJAX. The values are correctly returned in the array, confirmed by checking the console.log. Now my question is, how can I treat each array as an individual array ...

The webpage becomes unresponsive and gets stuck when sending a stream of requests to the web server via ajax

I am currently working on creating a signal on the webpage that displays either a green or red color based on values retrieved from a database. However, when I send a request after 10 seconds, the page appears to freeze and becomes unresponsive. I am strug ...

Synchronize data bidirectionally between parent and child components in Vue 2

This special wrapper I created for the Vue-multiselect package acts as a child component within this scenario. <template> <div> <multiselect v-model="items" :options="filteredList" ...

What are the steps to integrate the isotope-layout into a next.js project?

I have been exploring the implementation of isotope-layout in a next.js project. To accomplish this, I referred to the following blog post: https://stackoverflow.com/questions/25135261/react-js-and-isotope-js. Additionally, I found a useful codesandbox lin ...

Issue with Jquery AJAX success function specifically in Firefox browser, while other functions in the script are functioning correctly

I have 4 scripts using ajax, but one of them isn't functioning properly in Firefox. Even the alert in success doesn't trigger anything. There are no error messages, just nothing happening. However, it works perfectly fine in IE and Chrome. Belo ...

The second occurrence of a jQuery event

When a user left-clicks on the menu, it should load a view in a draggable box. However, the functionality is not working as expected. Sometimes you need to click twice - the first time the box appears but is not draggable, and the second time a new box app ...

What is causing a single state update when setState is called twice in React?

It seems like I'm making a beginner mistake in React as I am trying to call the "addMessage" function twice within the "add2Messages" function, but it only registers once. I believe this issue might be related to how hooks work in React. How can I mod ...

Item 'unreachable' displayed in Firefox console

I am working with several divs that have the class='class_name'. I have also defined var A = document.getElementsByClassName('class_name'); console.log(A[0]); Upon checking in the Chrome console, it displays: <div class="class_n ...

Javascript functions triggering repeatedly

I'm working on creating a fun trivia quiz using JavaScript functions. Each question involves globally defined functions called "right" and "wrong" that are supposed to update the score accordingly. However, I've run into an issue where the quiz s ...

Is the Site Header displayed depending on the scroll position and direction of scrolling?

On my website, I have a header that I want to hide when the user scrolls down 100px and show again when they scroll up 50px. I attempted to write a script for this functionality, but it doesn't seem to be working as expected. CSS /* This CSS rule w ...

The optimal method for storing tokens in Vue when using Laravel Sanctum

I am currently working on an application built with Laravel and Vue.js. My current focus is implementing the login/logout functionality using Laravel Sanctum. Here is my scenario: I already have the backend methods for login/logout/register set up, but I ...

This code snippet results in the property being unrecognized

I recently wrote this block of code and encountered an error while trying to run the alert function. It keeps telling me that 'this.words' is not defined. I suspect the issue lies within the jQuery portion, as I am able to access the array where ...

Where do the props in React come from?

In my quest to learn React. I perused w3school's guide, but alas, the specific case I seek was not found. Even Google failed me in this pursuit, leading me here to pose my question. Behold, the code in question: <FooComponent foo="bar"> {( ...

Is there a straightforward method in Excel VBA for choosing several worksheets that are already organized in an array?

I have been working on a code that generates multiple copies from a worksheet. Each new copy gets assigned as the next Worksheet type variable in an array: Dim wsv() as Worksheet Dim ddf as Integer, i as Integer 'After some processing, ddf will deter ...

JavaScript client side event that references the sender object ID in an aspx file

Can someone provide assistance with an issue in the code block below? function(s, e) { form1.hfRaiseEvent.value = s.ID; } This particular function is triggered when the Client Side Click Event occurs. From my understanding, s refers to the Sender Obj ...