Creating a Tic Tac Toe game using Javascript程序

As a beginner in programming, I am working on an assignment to create a Tic Tac Toe program using HTML and JavaScript. I found some code related to Tic Tac Toe on a website, but I am having trouble understanding how it works. Here is the code snippet:

if (document.all||document.getElementById){
    document.write('<style>.tictac{')
    document.write('width:50px;height:50px;')
    document.write('}</style>')
}

In this code snippet, the "tictac" class selector applies to buttons. Can someone provide clarification on what this code does exactly?

I appreciate your help and time.

Answer №1

The following JavaScript code is not specifically related to Tic-Tac-Toe, but rather serves a general purpose in checking for the presence of document.all and getElementById functions.

In today's world, both document.all (which lists all elements) and document.getElementById (a function that returns an element with a matching ID) are commonly available.

After checking for these functionalities, the code dynamically writes CSS styling information into the document using HTML tags.

<style>
 .tictac{
       width:50px;
       height:50px;
 }
</style>

To illustrate how it works, there is a snippet below where a div with a class of tictac is added and styled with a black background color.

Upon running the snippet, the size of the div is set to 50x50 pixels.

The code snippet includes additional logic:

A check to write CSS styles if either document.all or document.getElementById is present.

if (document.all||document.getElementById){
  document.write('<style>.tictac{')
  document.write('width:50px;height:50px;')
  document.write('}</style>')
}

This also includes pre-defined CSS styles for the .tictac class:

.tictac{
  background-color:#000;
}

And a sample usage of the styled div:

<div class="tictac">ABCD</div>

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

JavaScript UDP pinger timeout using the dgram module for nodejs

Currently, I am enrolled in a course where we are tasked with coding a UDP pinger using Javascript with Node.js and Dgram. The assignment provided to us is as follows: Our objective is to create the client-side code for an application. This client should ...

Issues with embedding iframes

I have a webpage with an embedded iframe that is making ajax requests. However, when I click on a link, it opens in the iframe instead of the main window. I tried using this JavaScript code: window.parent.location.href = url of link but it didn't wo ...

Would adjusting the font sizes of headings be crossing into grey or black hat SEO territory?

Here's the scenario: In this case, the page title is designated as H3, the article title is labeled as H2, and a certain keyword or important sentence is identified as H1 within the content. These elements have custom CSS classes applied to them, cau ...

Issues with dependencies in npx create react app

After initiating a new react project with npx create-react-app client, I have encountered an issue where the react-scripts command is not found, preventing me from running the start script. Is there a way to resolve this problem? Furthermore, when attempt ...

Transferring data between functional components in ReactJS and dealing with the output of [object Object] or undefined

I'm struggling with passing a prop in functional components that are both located in the same .js file. I previously attempted to seek help for this issue, but unfortunately, it wasn't productive. My goal is to extract the member_id from the GET ...

Enhancing user experience: Implementing specific actions when reconnecting with Socket.io after a disconnection

I am working on a game using node.js and socket.io. The code I have written is quite simple. The game starts animating as soon as it connects to the server and continues to do so. My main concern is ensuring that the game handles network and server disco ...

Tips for choosing an option from a dropdown menu and triggering the onchange event using Jquery

I'm facing a challenge in selecting dropdown options dynamically based on data received from a social networking plugin. I also need to trigger an onchange event to populate successive dropdown menus. The data I am working with includes country, stat ...

Running two servers at the same time using nodemon might seem complex, but it

Is it possible to run two servers (www.js and apiServer.js) simultaneously using nodemon? I have set the value for the "start" key in package.json as https://i.stack.imgur.com/r8M7p.jpg After running nodemon in the command prompt with the current working ...

Passing parent HTML attributes to child components in Angular 2

Is there a way to pass HTML attributes directly from parent to child without creating variables in the parent's .ts class first? In the sample code below, I am trying to pass the "type=number" attribute from the parent to the app-field-label component ...

Filtering an array in React to match parameters value

I am working on developing an e-commerce web application using react-bootstrap. My goal is to display different items based on their category, so if the URL is product/men'sclothing, I want to filter my array and only show products that belong to the ...

The scripts within the body tag are failing to load

After trying to embed angular into the body tag, I noticed that nothing is loading up. Upon inspecting the resources panel, I found that only files from the head are present. Moving all the scripts to the head section resolves the issue and everything load ...

Option and Select elements in iOS have unique font sizes

I noticed that the font size of my option is different from my select in Chrome PC compared to IOS. The sizes are exaggerated in the snippet provided. I experimented with and without using optgroup. .styled-select { overflow: hidden; height: 74px; float ...

Can one utilize Javascript to write in plain text format?

Currently, using JavaScript I have a plain text containing data that is displayed within my form tags. Everything is functioning correctly, but now I need to update the values inside the code of my form tags in order for the changes to also be reflected in ...

Having trouble persisting my login status in Selenium using Python

Has anyone experienced issues with logging into Instagram using an automate tab? Previously, I didn't have any problems, but now it seems that Instagram is not allowing users to log in through automation. An error message stating the following appears ...

The CSS stylesheet is not being applied to the components in Next.js

I am currently facing an issue with styling my React/Next component. Despite trying to apply a stylesheet to enhance its appearance, the CSS doesn't seem to be taking effect. Could someone please provide guidance on how I can resolve this? I have att ...

Is WebStorm with Node Supervisor being utilized to eliminate the need for restarting after every code modification?

Currently, I am utilizing WebStorm as my node IDE and have found it to be quite impressive. However, one issue I am facing is figuring out how to incorporate node supervisor into my workflow within WebStorm. Has anyone successfully managed to set this up ...

What results can be expected from a piped file stream?

Perhaps the wording of the question may not be perfect, but here is some additional context. With GridFSBucket, I am able to store a file in MongoDB and retrieve a download stream for that file. Here's my query: If I wanted to send that file back as a ...

Issues with single and double quotation marks in JQuery

I'm having trouble inserting a tag into the page. The tag contains both single and double quotes, so I tried storing that part in a variable named content. Can someone explain why this content variable isn't displaying onclick after the code runs ...

Differences in results between using .length with jQuery selectors and vanilla JS selectors

Examining the code snippet below from an HTML page: <form> <select id="select"> <option></option> <option></option> <option></option> </select> </form> When using ...

Is there a way to place my searchbox in the top right corner of the page?

I am currently working on creating a search function for my list of products. However, I have encountered an issue where the searchBox is not appearing in the top right corner as intended. Despite trying various solutions, I have been unsuccessful in movin ...