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.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can someone assist me what's wrong in my code. I'm try to convert JavaScript code to C# code
public class SplitString
{
public static string[] Solutions(string str)
{
arr = [];
for(var i = 0; i < str.Length; i += 2){
second = str[i+1] || '_';
arr.push(str[i] + second);
}
return arr;
}
}
And i encounter this error
src/Solution.cs(5,11): error CS1525: Invalid expression term '['
src/Solution.cs(5,12): error CS0443: Syntax error; value expected
javascript and C# are completely different languages; .NET / C# arrays are fixed size - so, you might want a list here:
var arr = new List<string>();
for(var i = 0; i < str.Length; i += 2){
var second = str[i+1]; // || '_'; <== this on the right makes no sense in C#
arr.Add(str[i] + second);
}
return arr.ToArray();
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>
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
var y= '110001'.split("").reverse();
var sum = 0;
for (var i = 0; i < y.length; i++) {
sum += (y[i] * Math.pow(2, i));
}
console.log(sum);
It would be simplest to do
console.log(Array.from('110001').reduce((prev, cur) => prev << 1 | cur));
<< is the left-bitshift operator, which here essentially multiplies by two.
Array.from (if available) is preferable to split. In this case it doesn't matter, but split will fail with surrogate pair characters such as 🍺, while Array.from will handle them correctly. This could also be written as [...'110001'], which ends up being the same thing.
Of course, you could also just say
parseInt('110001', 2)
check this snippet
var binary = '110001'.split("").reverse();
var sum = binary.reduce(function(previous, current, index) {
previous = previous + (current * Math.pow(2, index));
return previous;
}, 0);
console.log(sum);
Hope it helps
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>