Select a random string from an array [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript: Getting random value from an array
Could someone help me on this topic? I've got this code.
var textArray = [
'song1.ogg',
'song2.ogg'
]
audioElement.setAttribute('src', textArray);
How can I randomly get one of those strings into my audio element?
Would be glad if someone can help....

var textArray = [
'song1.ogg',
'song2.ogg'
];
var randomNumber = Math.floor(Math.random()*textArray.length);
audioElement.setAttribute('src', textArray[randomNumber]);

var randomIndex = Math.floor(Math.random() * textArray.length);
var randomElement = textArray[randomIndex];

Related

How to replace dot with a comma in the number [duplicate]

This question already has answers here:
Javascript toFixed localized?
(4 answers)
Closed last month.
Hey dear Stackoverflow community,
I have a JS code that calculates various values ​​based on an input value. It is output with a dot, but it should be displayed with a comma.
How can I customize the code for this?
<script>
var input = document.getElementById("invest_input-1");
input.oninput = function() {
var out = ((input.value * 0.38));
document.getElementById('invest_output_result-1').innerHTML = out.toFixed(2);
var out = ((input.value * 0.032));
document.getElementById('invest_output_result-2').innerHTML = out.toFixed(2);
var out = ((input.value * 0.03));
document.getElementById('invest_output_result-3').innerHTML = out.toFixed(2);
var out = ((input.value * 0.13));
document.getElementById('invest_output_result-4').innerHTML = out.toFixed(2);
};
</script>
Thank you so much!
Kind regards, Fabian
Based on the user's locale:
new Intl.NumberFormat().format(3500)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat#basic_usage
You can use
out.toFixed(2).replace('.', ',');

How can i fix my output will be 2 decimal Numbers ex:(.11) or it should be (.00) [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 5 years ago.
i want to get the output "total_amounts" like,ex(255.545645 = 255.54) and (150 =150.00). This is my code
$('.topag').on('input', function(){
var parent = $(this).closest('.calt');
var totalAmt = parseFloat(parent.find('.totalpr').val());
var quantity = parseFloat($(this).val());
parent.find('.total_amount').text(totalAmt*quantity);
$('#total_amounts').val(totalAmt*quantity);
$('#total_amounts').val(total);
calcul_total_quatities();
How can i fix this issue.
var total = 23.64654465;
var total_dec = total.toFixed(2);
document.getElementById("load").innerHTML = total_dec;
<p id="load"></p>
You can use the toFixed() method. The details for this can be found at the following link. See code below for coding needed for your scenario
var totalqty = totalAmt*quantity;
var total = totalqty.toFixed(2);
document.getElementById("total_amounts").innerHTML = total;

Split a string into two using pure javascript [duplicate]

This question already has answers here:
How to use split?
(4 answers)
Closed 7 years ago.
I have a string like this 132+456 or 132-456 or 132*456..etc ,it changes dynamically but I need to split this into 132 and 456 how to do it using pure java script?
It should be as easy as:
var parts = '132+456'.split('+');
parts[0]; //132
parts[1]; //456
Like so
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
// document.writeln only for example
document.writeln(a);
document.writeln(b);
Java
String[] values='132+456'.split('+');
String firstPart=value[0]; // has 132
for js
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];

Issue in URL Splitting [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have write following code to split the URL
var href = document.URL;
var GlobalBoomGuid = href.split("=");
var optionid = GlobalBoomGuid[1];
If i have URL like this : www.mydomain.com?optionid=655 It's giving me "655" .
But in current scenario my URL is www.mydomain.com?optionid=655#&ui-state=dialog and it's returning me 655#&ui-state
Now i want only id . How should i do that ?
Please if you dont like the question don't mark as a Negative
Thanx in Advance :)
This gets your result
var href = document.URL;
var foo = href.split("=")[1];
var GlobalBoomGuid = foo.split("#")[0];
var optionid = GlobalBoomGuid;
fiddle

How to get client MAC address by using javascript? [duplicate]

This question already has answers here:
MAC addresses in JavaScript
(8 answers)
Closed 9 years ago.
advance thanks those who gives me the great tips about how to find Physical address by using javascript.
Hope this helps!
function networkInfo(){
var wmi = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = wmi.ConnectServer(".");
e = new Enumerator(service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"));
for(; !e.atEnd(); e.moveNext()) {
var s = e.item();
var macAddress = unescape(s.MACAddress);
}
return macAddress;
}

Categories