Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to understand how are these two pieces of code different.
var bill=10.25+3.99+7.15;
var tip = bill*0.15;
var total=bill+tip;
total = total.toFixed(2);
console.log("$"+total);
And
var bill=10.25+3.99+7.15;
var tip = bill*0.15;
var total=bill+tip;
console.log("$"+total.toFixed(2));
Explanation in comments:
<script>
var bill=10.25+3.99+7.15;
var tip = bill*0.15;
var total=bill+tip; // total is number
total = total.toFixed(2); // total has been converted into a string with only two decimal places
console.log("$"+total); //prints out the '$' along with value of total variable which is a 'string'
typeof total; //returns "string"
</script>
<script>
var bill=10.25+3.99+7.15;
var tip = bill*0.15;
var total=bill+tip; //total is number
console.log("$"+total.toFixed(2)); //even after this statement the type of 'total' is integer, as no changes were registered to 'total' variable.
typeof total; //returns "number"
</script>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a message, received via an input field. For example:
This is *red* colour.
I want to replace everything between the two asterisks with a blank line ("___") so the outcome would be:
This is ___ colour.
How can I achieve this?
function myFunction() {
var str = "This is *red* color";
var startIndex = nthIndex(str,'*',1);
var endIndex = nthIndex(str,'*',2);
var output = str.replace(str.substring(startIndex, (endIndex+1)), "_");
}
function nthIndex(str, pat, n){
var L= str.length, i= -1;
while(n-- && i++<L){
i= str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to make an algorithm, for a NodeJS app, that converta any given string to a 1 to 3 digit number (better if the number is between 1-500).
e.g
ExampleString -> 214
Can anyone help me find a good solution?
EDIT:
I want to get a crime coefficient number from a username (string).
Ok, you can use JS function to get charCode of letter
let str = "some string example";
let sum = 0;
for (let i=0; i<str.length; i++) {
sum += parseInt(str[i].charCodeAt(0), 10); // Sum all codes
}
// Now we have some value as Number in sum, lets convert it to 0..1 value to scale to needed value
let rangedSum = parseFloat('0.' + String(sum)); // Looks dirty but works
let resultValue = Math.round(rangedSum * 500) + 1; // Same alogorythm as using Math.random(Math.round() * (max-min)) + min;
I hope it helps.
So as you are using nodejs, you can use crypto library to get md5 hash of string and then get it as HEX.
const crypto = require('crypto');
let valueHex = crypto.createHash('md5').update('YOUR STRING HERE').digest('hex');
// then get it as decimal based value
let valueDec = parseInt(valueHex, 16);
// and apply the same algorythm as above to scale it between 1-500
function coeficient() {
return Math.floor(Math.random() * 500) + 1;
}
console.log(coeficient());
console.log(coeficient());
console.log(coeficient());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
https://jsbin.com/wujusajowa/1/edit?html,js,output
I can sum the numbers of options. Like (5+5+5=15)
But I don't know a way to multiply the input with the sum of selects.
For example, What should I do to do 6 x (5+5+5) and get 90 ?
Use <input type="number">, define a global variable to store value of <input>; attach change event to <input> element to update global variable; use variable at change event of <select> element if variable is defined, else use 1 as multiplier
var input = 0;
$('select').change(function(){
var sum = 0;
$('select :selected').each(function() {
sum += Number($(this).val());
});
$("#toplam").html(sum * (input || 1));
}).change();
$("#miktar").on("change", function() {
input = this.valueAsNumber;
});
jsbin https://jsbin.com/cujohisahi/1/edit?html,js,output
Here's a simple example:
var mySum = 6 * ( 5 + 5 + 5 );
document.getElementById('result').innerHTML = mySum;
<div id="result"></div>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
When I tried with additions of variables I saw that:
https://jsfiddle.net/tyfyLsw9/
I think it's because this doesn't contain an integer.
var month = $("#monthd").val();
var J = 1;
var D = 8;
var K = J + D;
var U = J + month;
As you can see in fiddle J + month returns 110 instead of 11, why?
its a string, so the number you are adding gets coerced into a string as well. "10" + "1" = "101";
simply wrap the value returned in a Number Construct
var month = Number($("#monthd").val());
additionally you can use parseInt if the values are integers.
var month = parseInt($("#monthd").val(), 10);
the , 10 is important to parse it with base 10.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following syntax.
var name = [Name_is][234]
var number = find [234];
How can i find this number in javascript/jquery which is inside [] ?
Use a regular expression.
var string = "[Name_is][234]"
var matches = string.match(/\[(\d+)]/);
if (matches.length) {
var num = matches[1];
}
Check out a working fiddle: http://jsfiddle.net/rEJ5V/, and read more on the String.match() method.
if you want to extract the text between the [ ], you can do:
var name = "[Name_is][234]";
var check= "\{.*?\}";
if (name.search(check)==-1) { //if match failed
alert("nothing found between brackets");
} else {
var number = name.search(check);
alert(number);
}