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>
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 2 years ago.
Improve this question
I am trying to calculate the 10% from total array of values
for eg
perc = [56,50];
function percentage(perc) {
return (perc / 100) * 10;
}
You need to first sum the array, and then calculate 10% of that sum:
function percentage(perc) {
const sum = perc.reduce((a, b) => a + b, 0)
return sum * 0.1;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have the following code:
// Reference to the <div> which displays the random number:
var rndDiv = document.getElementById('rndNum')
// Reference to the <button> which generates the random number:
var rndBtn = document.getElementById('rnd')
// Generating the random number through 'click' eventlistener:
rndBtn.addEventListener('click', function intRnd() {
var n = Math.floor((Math.random() * 10) + 1);
console.log(n)
rndDiv.innerHTML = n
})
how could/should i write this code differently, how would you write it? Would you use, for example, arrow functions? let instead of var? I'm just curious. Also i'm the total opposite of a 'pro'-coder, just a beginner, and would like to read your code to this solution.
Thanks for taking your time and reading my post!
Here you go ... !
IIFE
Arrow Function
Let
(function() {
let rndBtn = document.getElementById('rnd');
rndBtn.addEventListener('click', () => {
let rndDiv = document.getElementById('rndNum');
rndDiv.innerHTML = Math.floor((Math.random() * 10) + 1);
});
})();
<button id="rnd">Click</button>
<div id="rndNum"></div>
Here is another way
const randomNumGenerator = () => Math.floor((Math.random() * 10) + 1);
const randomNumDiv = document.getElementById('rndNum');
document.getElementById('rnd').addEventListener('click', () => {
randomNumDiv.innerHTML = randomNumGenerator();
});
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
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
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.