How to parse a Javascript +random Number into .style.backgroundImage URL - javascript

Hi, I would like to get your opinion about this example.
Considering me as a beginner of coding.
I would like to add a random number after the URL GIF-File-Name.
Ex.:
Turn "intro_animation.gif" into "intro_animation.gif?v=7463" The number at the end should be random each time the browser refreshes, so the animated GIF can start again from the beginning.
I've seen many similar examples on stackoverflow.com, but I dont know how to put the parts together
...
Here is my code, which obviously don't work.
I want it to keep it as simple a possible.
<script>
setTimeout(function () {
document.getElementById("intro").style.backgroundImage = "url('./anim/intro_animation.gif' + ?v= + (Math.random() * 1000); )";
}, 0);
</script>
<div id="intro" class="intro_animation_gif" style="background-image: url('./anim/intro_animation.gif');">
</div>

You are not too far off. Make sure you put double quotes around the parts that are strings and concatenate with pluses.
You might also want to round the random number to make sure it does not contain decimals.
Sample:
document.getElementById("intro").style.backgroundImage =
"url('./anim/intro_animation.gif?v=" + Math.round((Math.random() * 1000)) + "')";

Related

How can I avoid decimals in a number value that is shown with innerHTML

The code is for an audio system when the user presses Up arrow or Down arrow then the volume changing with 0.05 up or down (5%) on the audio volume. That code isn't important. I just said because the changing value is 0.05 that means when I want to display the current volume the numbers sometimes look like this:
A good one:
A bad one:
Anything I could use? Maybe RegEx? (but how) Here is the code:
Element.innerHTML = "Hangerő: <strong>" + (audio_element.volume * 100) + "%</strong>"
Try parseInt() .
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
Element.innerHTML = "Hangerő: <strong>" + parseInt(audio_element.volume * 100) + "%</strong>"

How to display a random number between 2 numbers in Matter.js?

I am working in Matter.js, and I am not able to display a number between 2 numbers. I want help with what should be the correct code and in which function to write it.
I have no experience in Matter.js but this should work as this is pure javascript.
var randomNum = Math.floor(Math.random() * maxNum) + minNum;

Creating random number on fractional part between two numbers

I am working on the following code. How can I create random number between 150.570 and 150.720?
As you can see the integer-part (150) is always fixed and I just need to get random on fractional-part (between .570 to .720) only.
var gapLeft = Math.floor(Math.random() * 150.720) + 150.570 ;
console.log(gapLeft);
Here is your solution for this,
console.log(150.57+Math.random()*(0.72-0.57));

jQuery divide a number but keep decimals

I'm hoping for a bit of help with this issue I have.
I basically have a number... say 3000 and I want to display 30.00 on the front end without changing my variable (lots of reasons why I can't change it, I've just simplified this for the question)
HTML:
<span class="number"></span>
JS:
var three = 3000;
$('.number').append(three/100);
This returns '30' to my span. How can I keep the decimal points?
Thanks!
Number.toFixed() should do the trick:
var three = 3000,
result = (three/100).toFixed(2);
$('.number').append(result);
If you're concerned about toFixed() rounding, there are a lot of SO threads covering that topic.

How to add a trailing zero to a price?

I have a script which returns a price for a product. However, the price may or may not include trailing zeros, so sometimes I might have 258.22 and other times I might have 258.2. In the latter case, I need to add the trailing zero. How would I go about doing this?
You can use javascript's toFixed method (source), you don't need jQuery. Example:
var number = 258.2;
var rounded = number.toFixed(2); // rounded = 258.20
Edit: Electric Toolbox link has succumbed to linkrot and blocks the Wayback Machine so there is no working URL for the source.
Javascript has a function - toFixed - that should do what you want ... no JQuery needed.
var n = 258.2;
n.toFixed (2); // returns 258.20
I don't think jQuery itself has any string padding functions (which is what you're looking for). It's trivial to do, though:
function pad(value, width, padchar) {
while (value.length < width) {
value += padchar;
}
return value;
}
Edit The above is great for strings, but for your specific numeric situation, rosscj2533's answer is the better way to go.

Categories

Resources