Is it possible to increment a number continuously and then halt using an if statement?

How can I make the number stop increasing when the variable num reaches a whole number? Currently, it increments by 0.01 before pausing again. This logic is executed every frame using requestAnimationFrame.

if (Math.floor(num * 100) / 100 == Math.floor(num) && pause < 50) {
        pause += 1;
    } else {
        pause = 0;
        num += 0.01;
}

To see the full code block, visit:
https://github.com/BootLegAidan/Geometrical-Thing/blob/master/Update.js

Answer №1

The problem seems to be with the condition `Math.floor(num * 100 ) / 100 == Math.floor(num)` returning `true` even when `num` has an extremely small decimal value, like 1.000000007.

To address this issue, you can directly compare `num` to its rounded value:

if (num == Math.floor(num) && pause < 50) {

By comparing in this manner, the condition will only return `true` if `num` is a whole number.

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

Sorry, the provided text is already unique as it is an error message

I'm currently using the react-highlight-words package to highlight text inputted into a textbox After checking out the react-highlight-words documentation, I noticed that they are using searchWords as an array. https://www.npmjs.com/package/react-high ...

The property cannot be set for an undefined element in Jquery UI slider tabs

There seems to be some extra space at the bottom of the slider tabs between the navigators and content. Additionally, I am facing a challenge in making the page responsive as the content size does not adjust when I resize the window. To see the changes, I ...

Manifest.json encountered an unexpected token

Recently, I deployed a react/express project on Heroku () and encountered some console errors. You can check out the errors in the Chrome console error messages. I tried researching the error, and it seems like I might need to make some changes in my mani ...

The functionality of Regex in JavaScript is not meeting my expectations

I've come up with a regular expression for Name validation that only allows characters like "_", "-", "'", and "." Here is the regular expression pattern: ^[a-zA-Z][a-zA-Z0-9\.\-_']{1,24}$ The issue is that it's allowing na ...

Troubleshooting: Node.js not receiving data from HTML form

I am currently facing an issue with my HTML form and Node.js server. Despite implementing a form that is supposed to send data to the server, no information is being transferred. The form successfully makes a request, but it fails to send any form data alo ...

Is it possible to change the style of an element when I hover over one of its children?

Encountered an issue while working with HTML and CSS: //HTML <div> <div class="sibling-hover">hover over me</div> </div> <div class="parent"> <div>should disappear</div> </div> ...

What makes Next.js API so special?

As I delve into Next.js, I find myself grappling with the concept of server-side rendering (SSR) and API usage. When is it appropriate to utilize the API folder within pages versus deploying my own server along with a database? Would there be any conflic ...

What is a different way to rearrange items within an Angular Controller without relying on orderBy in the HTML?

http://plnkr.co/edit/apwAQG9tczOUckbc9bya?p=preview https://i.sstatic.net/NcNYh.png Seeking to rearrange li elements in the plnkr above without using ng-repeat | orderBy:predicate:reverse syntax in the markup. The challenge is that new items are dynamic ...

Using AngularJS to filter options in a dropdown list and trigger a function with ng-change

I have a dropdown menu that is being formatted using filters to display the text in a certain way. I need to send the selected item's ID value to the controller instead of just the name: <select ng-model="my_field" ...

Tips for designing interactive tooltips for images

Are there any alternative methods for adding multiple tooltips on top of an image in a more elegant way, without relying on image maps? Ideally, a solution involving jQuery would be great, but not essential. I am aware that image maps can be used for this ...

I am looking to remove the target attribute from an anchor tag if it does not have a value assigned

To ensure W3C validation, I must remove the target attribute from all anchors where the target value is null. Here is the code snippet inside the body: <div> <a href="#" target="">home empty</a> <a href="#" target="blank">home&l ...

Is it possible to minify HTML in PHP without parsing JavaScript and CSS code?

After finding a solution in this discussion, I successfully managed to 'minify' HTML content. function process_content($buffer) { $search = array( '/\>[^\S ]+/s', // eliminate spaces after tags, except for ...

The cursor remains positioned below the video within the designated div

I'm currently facing an issue in a project where the cursor div I created stays underneath the video element. No matter what I do, I can't seem to bring it to the front. Even setting a z-index on the video tag hasn't helped. Can someone plea ...

Summing columns in BigQuery using Case When statements

I am trying to calculate the total sum of a column based on a specific condition. Below is my code snippet: SELECT SUM(CASE WHEN properties = 'revenue' THEN property.value ELSE 0 END) AS TotalRevenue, FROM deneme-319116.events.eventstable; Sa ...

The d3.select function is failing to update the chart on the website

I am facing a challenge in updating data in a d3 chart with the click on an HTML object #id. After successfully coding it in jsfiddle, I encountered issues when implementing it on a web page. The scenario involves a simple leaflet map where the chart is d ...

Locate integer and add a decimal point followed by two zeros

I want to add .00 to a number if it is not already a decimal. However, the code I am using below seems to be converting the entire number to 0.00 instead of just appending .00 at the end. For instance, if the number is 12,200, it ends up as 0.00 after appl ...

Tips for enhancing the clarity of elements in KineticJS

I'm new to using Kinetic JS, and while I think it's a great library, I'm having trouble with the resolution of the elements. I know that Kinetic JS doesn't use SVG, but the resolution is not up to par. I'm creating circles and othe ...

JavaScript never forgets to validate the user input

Forgive me for my lack of experience, but I am new to this and seeking guidance. I am struggling to find a straightforward example on how to validate HTML input using JavaScript. Currently, I am working on a search function and need help in implementing ...

Generate Sub Menu for Navigation Bar based on Main Menu Selection

I'm currently working on dynamically setting menu items and sub menu items using React Props. My code successfully iterates through the main list items navItems.label and displays them. However, I'm facing an issue with displaying the sub menu i ...

Personalizing a Doughnut Graph

Currently in the process of creating a donut chart I am looking to achieve a design similar to the image below, where the values are displayed within the colored segments import DonutChart from 'react-d3-donut'; let data = [{ count ...