Creating a JavaScript function for a custom timed traffic light sequence

Currently, I am facing a challenge with my high school project of creating a timed traffic light sequence using JavaScript for my upcoming exams. Unfortunately, I feel quite lost and unsure about how to proceed. My initial plan involves formatting the values in my array (red, yellow, green) into distinct lights through CSS styling.

I've been attempting to code the red light as a practice exercise, but so far here is what I have:

    <DOCTYPE html>
    <html>
    <body>
    <script type = "JavaScript">
    var myLights = ["red", "Yellow", "green"]
    </script>
    <script type = "CSS">
    red {
    border-radius: 50%;
    width: 200px; 
    height: 200px; 
    color: #ff0000;
    }
    </script>
    </body>
    </html>

Not surprisingly, this code hasn't produced any visible results when viewed in Google Chrome.

If anyone out there could offer me some guidance or advice on how to improve my project, I would be extremely grateful.

Answer №1

I hope that nobody will complete this assignment for you as it is crucial for your learning journey to do it on your own. However, the individuals here might provide some general guidance when needed.

Here's a suggested approach to solving this problem:

  • Begin by creating three lights using static HTML and CSS. You can use classes like light (for all lights) and then red, amber, and green for each individual light.
  • To define them, consider using something like .red {} instead of red {} as you currently have.
  • You can establish common attributes for all lights using .light {} and then apply those to all the lights.
  • Each light will likely be represented by a <div> element which will require one or more classes. For example, your first light could be coded as
    <div id="red_light" class="red light"></div>
    .
  • It may be helpful to add a fifth class called on where red light represents off and red light on signifies on. Using properties like opacity can help control colors in this case. There are multiple approaches to achieve this effect.
  • Once you're satisfied with your HTML and CSS, incorporate some JavaScript. Think about how to transition between states and consider planning this out on paper before implementation. Then write functions to add and remove classes from each of your div elements.

A useful tip I still utilize is to continuously test small bits of code even when it's tempting to write larger chunks before testing. Observing immediate changes is mentally beneficial and provides reassurance to developers that they are progressing in the right direction.

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

Extra Pathway

Having trouble adding a second route to the /privacy page. The error "Cannot GET /privacy.html" keeps popping up. Any suggestions? This code seems to be failing to display the privacy.html content app.get('/privacy', function(req, res) { res.s ...

Sending data with the request body using Express and Passport

I've created a login application using Express and Passport, but I'm having trouble directing the user to their specific user profile page. The goal is for users to fill out an HTML form and then be redirected to their own profile page (which dif ...

Excessive JSON formatting is consuming an excessive amount of storage space

As I work on developing a website that recommends locations to visit in NYC, I am facing an issue with saving JSON data in local storage. My goal is to allow users to add their own suggestions and eventually integrate MongoDB into the site. To build the si ...

Find the correct file path for input using JavaScript or Angular 2

How can I retrieve the full file path of a selected Excel file using either pure JavaScript or Angular 2? I am looking to have the user select an Excel file, which will then be sent to a C# WEB API controller for further processing. Currently, my setup is ...

Styling an input text using CSS and Jquery without relying on a parent

Styling CSS input text without a parent div Hello everyone, I am looking to remove the <div id="input_container"> from the code below. Is it possible to achieve the same result without that surrounding div? Check out the jsfiddle: http://jsfiddle.n ...

Tips to Avoid Multiple Executions of Javascript Code due to Caching

When I make a request to my Asp.net-MVC application, it returns a partial-view. public ActionResult AccountDetails() { return PartialView("~/Views/partials/Account/Details.cshtml"); } To load the partial view in my form, I use the following ...

The export "hasInjectionContext" is not provided by the source file "node_modules/vue-demi/lib/index.mjs", but it is being requested in the file "node_modules/pinia/dist/pinia.mjs"

Every time I launch my program, the console displays an error message stating that "hasInjectionContext" is not exported by "node_modules/vue-demi/lib/index.mjs", imported by "node_modules/pinia/dist/pinia.mjs" at line 6:9. I ...

Fetching a JSON object from an external URL using JavaScript

Currently, I am working on a project using JavaScript and have an API that provides me with a JSON Object. You can access this JSON object by clicking on the following link: . Within this JSON object, there is a specific element located at JSONOBJECT.posi ...

Is it true that Redux Saga's select function returns mutable state?

Is the state returned by Redux Saga's select function mutable or immutable? To learn more, visit https://github.com/redux-saga/redux-saga ...

Utilizing inline javascript to dynamically update the selected option of a radio button

A form on my website includes two radio buttons: one labeled 'trigger_unit' and the other labeled 'normal_unit'. In addition, there is a select drop-down menu with four values and a default NULL option. <select rel="dropdown" name=" ...

easy method for creating a hyperlink that triggers a "download" pop-up box

Is there a simple and efficient way to have some of my links trigger the 'save file as' prompt (similar to right-clicking) immediately after they are clicked for the first time? ...

Angular Kendo dropdownlist and input textbox are not working together as anticipated

I'm looking to implement a dropdown list similar to Google search using Kendo Angular. However, I've encountered an issue where entering text in the textbox and pressing "Enter" passes the first matching value from the dropdown list to my compone ...

Tips for controlling numerous tabSlideOUt tabs on a single webpage

Is there a way to implement multiple tabSlideOut functionalities on a single page, similar to the examples provided in the following links: source code on GitHub and Fiddle Example? Specifically, I am looking to have tabs that can be toggled, ensuring tha ...

Tips for including a decimal point in an angular reactive form control when the initial value is 1 or higher

I am trying to input a decimal number with 1 and one zero like 1.0 <input type="number" formControlName="global_velocity_weight" /> this.form = this.fb.group({ global_velocity_weight: new FormControl(1.0, { validators: [Valida ...

Ordering ng-repeat in AngularJS using a separate arrayDiscover how to easily order your

Imagine I have an array containing keys in a specific order orderedItems=["apple","banana","orange]; and there is a JSON object that I want to display using ng-repeat but following the sequence specified in the array: {"fruits": { "apple":{ ...

The issue of Three.js SkinnedMesh jittering arises when it is distant from the scene's root (0,0,0)

In my current project, I am developing a third-person RPG style game using Three.js and Blender. The terrain in the game world is tiled and loops endlessly in all directions, triggered when the player object approaches defined edges along the z or x-axis. ...

Is there a way to adjust the image opacity of a background using Material UI?

Is there a way to adjust the opacity of a background image using Material UI? I attempted to achieve this by utilizing the makeStyles hook in Material UI. Here is an example of my code: import React from 'react'; import {Box,Typography } from &ap ...

unexpected alteration of text sizing in mathjax within reveal.js presentations

Something strange is happening with the font size in my slides. The code for each slide is the same, but there is an unexpected change between the 3rd and 4th slide. I cannot figure out what is causing this discrepancy. Oddly enough, when I remove the tit ...

Connecting a string array to the navigation bar

Need Assistance with AngularJS: In my controller, I have a string array that looks like this: var app = angular.module('myApp', []); app.controller('mycontroller', function($scope) { $scope.menuitems =['Home','About&apos ...

Manipulating an HTML <div> element to display its value as a suffix in another <div> within the HTML code

I'm attempting to combine and show text along with content from another <div> as the modal-header, but it's not displaying correctly. When I give an ID to the modal-header, the *&times;* symbol disappears. This is how my modal-header l ...