I have a problem, in fact at least I do not know how. I have several fields revealing different amount each position is identified by an id example: p1, p2, p3 etc. ...
So this fields contain amounts, in their decimal form.
what I wish to make. Because in fact I will be led to allocate an amount entered in a field that is a deposit.
that is to say as long as the amount of deposit is> 0 then I spread over the various post
For example let's say I have
300 = p1, p2 and p3 = 120 = 50
and I have a deposit for an amount of 450 Euros, so I would
p1 = 0 and o1 fields (fields that is revealing what has been imputed) 300
p2 = 0 and the fields 120 o2
p3 = 20 fields and o3 = 30
I actually tried by a number of conditions but I tangled brushes.
this is what it gave:
<script type="text/javascript">
function ventilation()
{
var montantacompte = document.getElementById("montantacompte").value;
var p1 = document.getElementById("p1").value;
var p2 = document.getElementById("p2").value;
var p3 = document.getElementById("p3").value;
var p4 = document.getElementById("p4").value;
var p5 = document.getElementById("p5").value;
var p7 = document.getElementById("p7").value;
var p8 = document.getElementById("p8").value;
var p9 = document.getElementById("p9").value;
var p10 = document.getElementById("p10").value;
var p11 = document.getElementById("p11").value;
var p12 = document.getElementById("p12").value;
var p13 = document.getElementById("p13").value;
if(p1>0 &&p1-montantacompte>=0)
{
f1=p1-montantacompte;
document.getElementById('p1').value=f1;
document.getElementById('o1').value=Math.round(montantacompte*100)/100;
}
if(p1>0 &&p1-montantacompte<=(p1+p2))
{
reste1=montantacompte-p1;
impute=p1;
impute2=reste1;
f1=m1-impute;
document.getElementById('p1').value=f1;
document.getElementById('o1').value=Math.round(impute*100)/100;
document.getElementById('o2').value=Math.round(impute2*100)/100;
}
}
</script>
I also tried with a loop but well I also tangled brush, my small knowledge of javascript does not help.
<script type="text/javascript">
function ventilation()
{
var reste = document.getElementById("montantacompte").value;
var ac = document.getElementById("montantacompte").value;
var p1 = document.getElementById("p1").value;
var p2 = document.getElementById("p2").value;
var p3 = document.getElementById("p3").value;
var p4 = document.getElementById("p4").value;
var p5 = document.getElementById("p5").value;
var p7 = document.getElementById("p7").value;
var p8 = document.getElementById("p8").value;
var p9 = document.getElementById("p9").value;
var p10 = document.getElementById("p10").value;
var p11 = document.getElementById("p11").value;
var p12 = document.getElementById("p12").value;
var p13 = document.getElementById("p13").value;
var c = 1;
while(reste>0 && c<13)
{
if(reste> 'p'+c)
{
reste = 'p'+c - ac;
document.getElementById('p'+c).value=Math.round(honoraires*100)/100;
print reste;
}
}
</script>
suddenly, I do not know because every time I do not arrived more advancing in both cases.
in advance thank you to all and anyone who can help me progress.
Sincerely yours.
As I understand this issue, the user inputs a Payment Amount then this amount is applied to line items on an order.
Input: 17100
Item 1: <input id="p1" class="items" value="16000.00 €"/>
Item 2: <input id="p1" class="items" value="535.00 €"/>
Item 3: <input id="p1" class="items" value="955.00 €"/>
function getItems()
{
var items = new Array();
var itemCount = document.getElementsByClassName("items");
for(var i = 0; i < itemCount.length; i++)
{
items[i] = document.getElementById("p" + (i+1)).value;
}
return items;
}
function setItems(items,payAmt)
{
document.getElementById("inputField").value = payAmt;
for(var i = 0; i < items.length; i++)
{
document.getElementById("p" + (i+1)).value = items[i];
}
}
function itemSum(items)
{
var sum = 0;
for(var i=0; i < items.length; i++)
{
sum = items[i] + sum;
}
return sum;
}
function payment(inputElm)
{
var items = getItems();
var payAmt = document.getElementById("inputField").value;
var i = 0;
var sum = itemSum(items);
while(payAmt != 0 && sum != 0)
{
var temp = items[i] - payAmt;
if(temp > 0)
{
items[i] = temp;
break;
}
else if(temp < 0)
{
items[i] = 0;
payAmt = temp*-1;
}
i++;
sum = itemSum(items);
}
setItems(items, payAmt);
}
I think that should do it if I understand your problem correctly. You'll need to make sure the IDs match up and this is untested so there'll be a bug or two in there more than likely.
Related
I'm creating a table seating function in javascript, directly below, for a scenario where there is a population of P participants, in this case tested with 80, and S seats per table, in this case tested by 8, each participant may only visit each table once with a total of ten tables and each participant may not meet another participant more than once for a minimum of 10 rotations.
How can I make 10 unique sets of P/S, 10 times?
To explain my naming, arrayArray is the array for each of the tables and the outer arrays inner array elements are the table seating, arrayArrayPrevious is the list of everyone whose already been to a table, and the participantArray is an array of all possible participants.
The trouble seems to be when finding two participants have already met and moving the second one to the end of the participantArray to be tried again later results in only one participant ever being placed.
I'm placing the entire code below the function snippet in the event someone can help solve it and its useful for others in the future.
function notMetAlready(w, arrayArray, participantAlreadyMet){
if(arrayArray.includes(participantAlreadyMet)){
var moveToEnd = arrayArray[w][0];
console.log(moveToEnd);
participantArray.push(moveToEnd);
console.log(participantArray);
return true;
//console.log(index);
}
if(!arrayArray.includes(participantAlreadyMet)){
return true;
}
else{
return false;
}
}
Full Code
<html>
<head>
<script type="text/javascript">
var tables = 10;
var participants = 80;
var participantPool = [];
var seatingPerTable = participants/tables;
var arrayArray = new Array(tables);
for(var i = 1; i <= participants; i++){
participantPool.push(i);
}
var count = 1;
var participantArray = new Array(participants);
var participantAlreadyMet = new Array(participants);
var arrayArrayPrevious = new Array(tables);
for (var i = 0; i <= tables; i++) {
arrayArrayPrevious[i] = [];
arrayArray[i] = [];
}
for (var i = i; i < participantAlreadyMet.length; i++) {
participantAlreadyMet[i] = [i];
}
function MatchingPairs() {
for (var i = 0; i < tables; i++) {
arrayArray[i] = [];
}
for(var h = 0; h < participants; h++){
participantArray[i] = i+1;
}
for(var w = 0; w < arrayArray.length; w++){
if(tablesHaveNotIncluded(w,0)){
// for(var n = 1; n < participants; n++){
do{
if(tablesDoNotInclude(0) && notMetAlready(w, arrayArray[w], participantAlreadyMet[0])){
arrayArray[w].push(participantArray[0]);
arrayArrayPrevious[w].push(participantArray[0]);
participantArray.shift();
}
}while(participantArray >= 0);
}
}
function notMetAlready(w, arrayArray, participantAlreadyMet){
if(arrayArray.includes(participantAlreadyMet)){
var moveToEnd = arrayArray[w][0];
console.log(moveToEnd);
participantArray.push(moveToEnd);
console.log(participantArray);
return true;
}
if(!arrayArray.includes(participantAlreadyMet)){
return true;
}
else{
return false;
}
}
for(var z = 0; z < tables; z++){
var plus = z + 1;
console.log("Table " + plus + " " + arrayArray[z] );
}
console.log("Rotation " + count);
count++;
function tablesHaveNotIncluded(w,n){
var outerArray = arrayArrayPrevious[w];
if(!outerArray.includes(n)){
return true;
}
return false;
}
function tablesDoNotInclude(n){
for(var w = 0; w < tables; w++){
if(!arrayArray[w].includes(n)){
return true;
}
}
return false;
}
}
</script>
</head>
<body>
<button onclick="MatchingPairs()">Combinations</button>
</body>
</html>
This function is freezing my page.
function findMode (array)
{
var modeArr = [];
var modeCounter = [];
modeArr.length = array.length;
modeCounter.length = array.length;
}
However, when I remove this it runs just fine.
modeArr.length = array.length;
modeCounter.length = array.length;
Here is all of my code:
<html>
<head>
</head>
<body>
<p> Please enter a series of numbers, each separated by a new line.<br><p>
<textarea id="myTextArea" rows = "7" cols = "50"></textarea><br>
<button onclick="processData()">Done</button>
<p id = "mean"></p>
<p id = "median"></p>
<p id = "count"></p>
<p id = "summation"></p>
<p id = "mode"></p>
<p id = "variance"></p>
<p id = "sd"></p>
<script type = "text/javascript">
var mean = 0;
var median = 0;
var count = length;
var mode = 0;
var variance = 0;
var standard_deviation = 0;
var meanOutput = document.getElementById('mean');
var medianOutput = document.getElementById('median');
var modeOutput = document.getElementById('mode');
var countOutput = document.getElementById('count');
var summationOutput = document.getElementById('summation');
var varianceOutput = document.getElementById('variance');
var sdOutput = document.getElementById('sd');
function processData()
{
var arrayOfLines = document.getElementById('myTextArea').value.split('\n');
var sum = findSum(arrayOfLines);
findMean(arrayOfLines, sum);
findMedian(arrayOfLines);
findMode(arrayOfLines);
findVariance(arrayOfLines);
findStandardDeviation(arrayOfLines);
findVariance(arrayOfLines);
}
function findSum (array)
{
var count = array.length;
var sum = 0;
for (var a = 0; a < array.length; a++)
{
sum += parseInt(array[a]);
}
countOutput.innerHTML = "Count: " + array.length;
summationOutput.innerHTML = "Sum: " + JSON.stringify(sum);
return sum;
}
function findMode (array)
{
var modeArr = [];
var modeCounter = [];
modeArr.length = array.length;
modeCounter.length = array.length;
for (var a = 0; a < array.length; a++)
{
for (var b = 0; b < modeArr.length; b++)
{
if (modeArr[a] == modeArr[b])
{
modeCounter[a]++;
}
if (a == 0)
{
b--;
}
}
modeArr[a] = array[a];
}
modeOutput.innerHTML = "Mode: ";
}
function findMean (array, sum)
{
mean = sum/array.length;
meanOutput.innerHTML = "Mean: " + mean.toPrecision(2);
}
function findMedian (array)
{
for(var i=0; i < array.length; i++)
{
array[i] = +array[i];
}
var sortedArrayOfLines = array.sort(function(a, b){return a - b});
if (array.length % 2 == 1)
{
median = sortedArrayOfLines[((array.length - 1)/2)]
}
else
{
median = (sortedArrayOfLines[array.length/2] + sortedArrayOfLines[(array.length/2)+1])/2
}
medianOutput.innerHTML = "Median: " + median;
}
function findVariance (array)
{
var mean = mean(array);
return mean(array.map(function(num)
{
varianceOutput.innerHTML = Math.pow(num - mean, 2);
}));
}
function findStandardDeviation (array)
{
medianOutput.innerHTML = Math.sqrt(variance(array));
}
</script>
</body>
</html>
So the issue isn't the length it's a infinite loop.
The problem is this bit of code
if (a == 0)
{
b--;
}
This is inside the following loop with b as the iterator. See below.
for (var b = 0; b < modeArr.length; b++)
a is set to zero by the outer loop. Thus a==0 is always true inside the inner loop. b will never increase only decrease. Thus this is a infinite loop because b will never be greater than modeArr.length.
So I would consider revising the function, below is a example of a possible candidate for a mode function:
Get the element with the highest occurrence in an array
I have the following applied as a library to a CRM 2013 form
function calcServicePriceTotal() {
alert("Start");//----------HERE
if (document.getElementById("Services")) {
alert("InsideIf"); //----------HERE
var grid = document.getElementById("Services").control;
alert("ThisFar?");//----------HERE
var ids = grid.Control.get_allRecordIds()
alert("ThisFar2?");//----------HERE
for (i = 0; i < ids.length; i++) {
alert("InsideFor");//----------HERE
var cellValue = grid.control.getCellValue('iss_salesprice', ids[i]);
var number = Number(cellValue.replace(/[^0-9\.]+/g, ""));
sum = sum + number;
}
Xrm.Page.data.entity.attributes.get("ava_tempgrossvalue").setValue(sum);
alert("Done");//----------HERE
}
else {
alert("Else");//----------HERE
setTimeout("calcServicePriceTotal();", 2500);
}
}
For some reason I get as far as the alert("ThisFar?") line but then nothing else happens.
Does that mean that there is a problem with var ids = grid.Control.get_allRecordIds()? I don't know why I'm not at least seeing "ThisFar2".
Can anyone see anything obvious?
function calcServicePriceTotal() {
if (document.getElementById("Services")) {
var grid = document.getElementById("Services").control;
var ids = grid.get_allRecordIds()
var sum = 0
for (i = 0; i < ids.length; i++) {
var cellValue = grid.getCellValue('iss_salesprice', ids[i]);
var number = Number(cellValue.replace(/\D/g, ''));
number = number/100;
sum = sum + number;
}
Xrm.Page.data.entity.attributes.get("iss_value").setValue(sum);
}
else {
setTimeout("calcServicePriceTotal();", 1500);
}
}
Final working solution
I have created a button using JavaScript, and I have a list that is supposed to get a random number when I "roll the dice"
I need to list of numbers to say "You rolled a 1" for example. How do I do that? And also I only need it to show the last 10 numbers.
var rollNumber = 0;
var values = [];
function dieRolled() {
rollNumber += 1;
var numRolled = Math.ceil(Math.random() * 6);
values.push(numRolled);
document.getElementById("results").innerHTML = "";
for (var x = values.length-1 ; x>=0; x--) {
var newRoll = document.createElement("li");
newRoll.innerHTML = values [x] +"You rolled a";
document.getElementById("results").appendChild(newRoll);
if (x == 11)break;
}
}
How about this?
var output = document.getElementById("Output");
var values = [];
function roll()
{
values.push(Math.ceil(Math.random() * 6));
// If the history is too big, drop the oldest...
if (values.length > 10)
{
values.shift();
}
// Rewriting the history log
var text = "";
for (var i in values)
{
text += "You rolled a " + values[i] + "\n";
}
output.innerHTML = text;
}
// Rolling multiple times
setInterval(function(){ roll(); }, 1000);
<pre id="Output"></pre>
Try this:
var list = document.getElementById('demo');
var count = 0;
function changeText2() {
count++;
if(count <= 10)
{
var numRolled = Math.ceil(Math.random() * 6);
var entry = document.createElement('li');
entry.appendChild(document.createTextNode("You rolled:"+numRolled));
list.appendChild(entry);
}
}
<input type='button' onclick='changeText2()' value='Submit' />
<p>Dices you rolled</p>
<ol id="demo"></ol>
I am using the below code for speed reading simulation.
Its working fine. But i have pasted 500 words into the box, after read all the words, words count is differing from the input words count.
$('#sim').each(function(){
this.contentEditable = true;
});
var go = $('#go');
var stop = $('#stop');
var wordCount = 0;
var wordCountBox = $('#wordCountBox');
var timepassed = $('#timepassed');
var textRead = $('#textRead');
go.on("click", function(e){
e.preventDefault();
startSim();
});
function startSim(){
var speed = $('#speed').val();
timeStart = $.now();
var sim = $('#sim').text();
var wordArray = sim.split(" ");
var simWrap = $('#sim');
var arrCount = wordArray.length;
var alreadyRead = [];
for (var i = 0; i < arrCount; i++) {
(function(index) {
setTimeout(function() {
var pos = index;
if(pos < 0){
pos = 0;
}
alreadyRead.push(wordArray[pos]);
wordArray[pos] = '<b>'+wordArray[pos]+'</b>';
var words = wordArray.join(" ");
simWrap.html(words);
wordCount++;
if(pos == (arrCount - 1)){
triggerDone();
}
}, i * speed);
})(i);
}
// Function done
function triggerDone(){
wordCountBox.text(wordCount+' Words Read');
var timeEnd = $.now();
var timeRes = timeEnd - timeStart;
timeRes = parseInt(timeRes);
timeRes = timeRes / 1000;
timepassed.text(timeRes+" seconds have passed");
alreadyRead = alreadyRead.join("");
textRead.text(alreadyRead);
var summary = $('#summary');
summary.show();
return;
}
stop.on("click", function(e){
e.preventDefault();
triggerDone();
});
}
#sim{
width:800px;
height:300px;
border:solid 1px #2e2e2e;
color:#2e2e2e;
padding:5px;
overflow:auto;
border:9px outset #0ADA0A;
margin-top:1em;
}
<div id="sim"></div>
Why it is calculating the words count?
And One more doubt, how to auto scroll, when reading the words.
Could Someone please solve this?
The first issue is with:
var wordArray = sim.split(" ");
In this case, it's creating a new "word" for the array each time a space is encountered... so a string like:
var words = "foo bar bim baz";
Will return and array with a length of 6. Use regex...
var wordArray = sim.split(/\s+/);
.. and it will solve this problem.