How to achieve this calculation using javascript - javascript

First of all thanks for viewing this question.Now,let me show you what I want
as you could see there are two columns in my table..
Now, if someone put 30 or 300 then the table will look like this
Basically,the calculation will go like this way.
Now, I am doing this calculation using javascript and I don't figure out how to get this.
Here, is my code that I have tried..
//NOTE: This is an asp grid
//enterdValue = the value I am putting.
function FirstPriority(sender, eventArgs) {
debugger;
var loop = true;
var loopcounter = 0;
if (isNaN($find("<%= txt_payingAmt.ClientID %>").get_textBoxValue())) {
eventArgs.set_cancel(true);
}
else {
let enterdValue = parseFloat($find("<%= txt_payingAmt.ClientID %>").get_textBoxValue());
var grid = $find("<%=rgv_INVList.ClientID%>");
//loop through..
for (var row = 0; row < grid.MasterTableView.get_dataItems().length ; row++) {
var remainingTot = 0;
loopcounter += 1;
var rowTotAmount = parseFloat(grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "GrandTotal").innerHTML);
var alreadyPaid = parseFloat(grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML);
if (loopcounter == 1) {
if ((alreadyPaid + enterdValue) <= rowTotAmount) {
grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML = alreadyPaid + enterdValue;
break;
}
}
else if (loopcounter > 1) {
if ((alreadyPaid + enterdValue) == rowTotAmount) {
grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML = rowTotAmount;
break;
}
else {
grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML = remainingTot;
break;
}
}
else {
if (loop) {
remainingTot = (alreadyPaid + enterdValue) - rowTotAmount;
grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row], "PayingAmount").innerHTML = rowTotAmount;
if (grid.MasterTableView.get_dataItems().length != loopcounter) {
if (remainingTot <= grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row + 1], "PayingAmount").innerHTML) {
grid.MasterTableView.getCellByColumnUniqueName(grid.MasterTableView.get_dataItems()[row + 1], "PayingAmount").innerHTML = alreadyPaid + enterdValue;
loop = false;
}
}
}
}
}
}
Let me tell you what these variables does,
enterdValue = This will get an input from user.
grid = rendered as table and to loop thru I need this.
rowTotAmount = TotalInvAmount as shown in grid.
alreadyPaid = paidAmount as shown in grid.
Help needed to achieve this :)
Thanks & regards

I have figured out to solve it :) hence posting it here.
function FirstPriority(args) {
debugger;
resetGrid();
var paidAmount, totAmount, paidAmountHdn, payingAmount, remainingBalance = 0;
payingAmount = parseFloat(args.value);
var grid = $find("<%=rgv_INVList.ClientID%>");
if (!isNaN(payingAmount)) {
remainingBalance = payingAmount;
for (var row = 0; row < grid.MasterTableView.get_dataItems().length ; row++) {
paidAmount = parseFloat(grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerText); //grid.MasterTableView.get_dataItems()[0]._element.cells[1].innerText
totAmount = parseFloat(grid.MasterTableView.get_dataItems()[row]._element.cells[4].innerText);
paidAmountHdn = parseFloat(grid.MasterTableView.get_dataItems()[row]._element.cells[7].innerText);
if (remainingBalance > 0) {
if (row == 0) {
if((paidAmount+payingAmount)>=totAmount)
{
remainingBalance = (paidAmount + payingAmount) - totAmount;
grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML = totAmount;
}
else if ((paidAmount + payingAmount) <= totAmount) {
remainingBalance = 0;
grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML = payingAmount+paidAmount;
break;
}
}
else {
if (remainingBalance > totAmount) {
grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML = totAmount;
remainingBalance = remainingBalance - totAmount;
}
else if(totAmount>=remainingBalance){
grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML = remainingBalance;
remainingBalance = 0;
break;
}
}
}
}
}
else {
for (var row = 0; row < grid.MasterTableView.get_dataItems().length ; row++) {
paidAmountHdn = parseFloat(grid.MasterTableView.get_dataItems()[row]._element.cells[7].innerText);
grid.MasterTableView.get_dataItems()[row]._element.cells[5].innerHTML = paidAmountHdn;
}
}
}

Related

JavaScript How to run variables in if and for?

I want to use FOR loop instead, please.
let int1, int1, int3;//... (int4 to int999)
let op1, op;//... (op2 to op999)
if(int1 < 200) {
op1 = int1;
}
else {
op1 = '';
}
if(int2 < 200) {... (same IF statement for int2 to int999)
Instead, I want this.
for(i = 1; i < 5; i++) {
let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];
if(int[i] < 200) {
op[i] = int[i];
}
else {
op[i] = '';
}
}
alert(op1 + op2 + op3 + op4);
But that doesn't work somebody help.
Your code should be as follows with an example:
let int = [1, 2, 3, 4];
let op = [999, 999, 999, 999];
for(i = 0; i < 4; i++) {
if(int[i] < 200) {
op[i] = int[i];
}
else {
op[i] = '';
}
}
alert(op[0] + op[1] + op[2] + op[3]);
Out:
285
Or in your kind of "symbolic form":
let int = [int1, int2, int3, int4];
let op = [op1, op2, op3, op4];
for(i = 0; i < 4; i++) {
if(int[i] < 200) {
op[i] = int[i];
}
else {
op[i] = 0;
}
}
alert(op[0] + op[1] + op[2] + op[3]);
Instead of a set of variables, You should use an array instead.
Then, Your code would look something like this:
let input = [input1, input2 ... input999];
let output = [];
for(let i = 0; i < input.length; i++){
if(input[i]) {
output[i] = input[i];
} else {
alert("error")
}
})

If and else both executed in nested loop

In google apps spreadsheet I am parsing json and filling up cells. After seting value to cell I check if the row has this value and if it does I go to the next cell and set new value, then I check the second value and so on. But something goes wrong and the if and else are both executing..
var customfields = SpreadsheetApp.openById('id').getSheetByName('Form').getRange('J25').getValue();
if (customfields == true) {
var jj = 2;
var ii = 1;
for each(item in data) {
var size = Object.keys(data[k].customfields).length;
if (size == 0) {
k = k + 1;
continue;
}
Logger.log(k)
for (j = 0; j < size; j++) {
var vl = data[k].customfields[j].value;
var nm = data[k].customfields[j].name;
var rng = SpreadsheetApp.openById('id').getSheetByName('Data').getMaxColumns();
for (var cn = 0; cn <= rng; cn++) {
var dat = sheet.getRange(1, cn).getValue();
Logger.log("IF STATEMENT")
if (dat == nm) {
sheet.getRange(ii, cn).setValue(nm);
} else {
sheet.getRange(ii, br).setValue(nm);
}
}
Logger.log(nm)
br = br + 1;
}
k = k + 1;
}
}

Need to split up a quiz written in javascript into 1 page per Q instead of all Qs on a single page

I have a quiz currently all on one page written in javascript. I need to rewrite/modify/split up this code so each question appears on its own page.
I used a tool called eXeLearning to build the quiz. This is part of a SCORM project that will be hosted on an LMS. I'm just not sure how to go about this. I'm good with HTML and CSS and I know a little PHP, but not javascript. Here is the script in question, I can also post the entire page of code if need be:
<script type="text/javascript">
<!-- //<![CDATA[
var numQuestions = 10;
var rawScore = 0;
var actualScore = 0;
var question0;
var question1;
var question2;
var question3;
var question4;
var question5;
var question6;
var question7;
var question8;
var question9;
var key0 = 0;
var key1 = 1;
var key2 = 0;
var key3 = 0;
var key4 = 1;
var key5 = 0;
var key6 = 0;
var key7 = 1;
var key8 = 0;
var key9 = 1;
function getAnswer()
{
scorm.SetInteractionValue("cmi.interactions.0.id","key0b0");
scorm.SetInteractionValue("cmi.interactions.0.type","choice");
scorm.SetInteractionValue("cmi.interactions.0.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key0b0[i].checked)
{
question0 = document.getElementById("quizForm0").key0b0[i].value;
scorm.SetInteractionValue("cmi.interactions.0.student_response",question0);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.1.id","key1b0");
scorm.SetInteractionValue("cmi.interactions.1.type","choice");
scorm.SetInteractionValue("cmi.interactions.1.correct_responses.0.pattern",
"1");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key1b0[i].checked)
{
question1 = document.getElementById("quizForm0").key1b0[i].value;
scorm.SetInteractionValue("cmi.interactions.1.student_response",question1);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.2.id","key2b0");
scorm.SetInteractionValue("cmi.interactions.2.type","choice");
scorm.SetInteractionValue("cmi.interactions.2.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key2b0[i].checked)
{
question2 = document.getElementById("quizForm0").key2b0[i].value;
scorm.SetInteractionValue("cmi.interactions.2.student_response",question2);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.3.id","key3b0");
scorm.SetInteractionValue("cmi.interactions.3.type","choice");
scorm.SetInteractionValue("cmi.interactions.3.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key3b0[i].checked)
{
question3 = document.getElementById("quizForm0").key3b0[i].value;
scorm.SetInteractionValue("cmi.interactions.3.student_response",question3);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.4.id","key4b0");
scorm.SetInteractionValue("cmi.interactions.4.type","choice");
scorm.SetInteractionValue("cmi.interactions.4.correct_responses.0.pattern",
"1");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key4b0[i].checked)
{
question4 = document.getElementById("quizForm0").key4b0[i].value;
scorm.SetInteractionValue("cmi.interactions.4.student_response",question4);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.5.id","key5b0");
scorm.SetInteractionValue("cmi.interactions.5.type","choice");
scorm.SetInteractionValue("cmi.interactions.5.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key5b0[i].checked)
{
question5 = document.getElementById("quizForm0").key5b0[i].value;
scorm.SetInteractionValue("cmi.interactions.5.student_response",question5);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.6.id","key6b0");
scorm.SetInteractionValue("cmi.interactions.6.type","choice");
scorm.SetInteractionValue("cmi.interactions.6.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key6b0[i].checked)
{
question6 = document.getElementById("quizForm0").key6b0[i].value;
scorm.SetInteractionValue("cmi.interactions.6.student_response",question6);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.7.id","key7b0");
scorm.SetInteractionValue("cmi.interactions.7.type","choice");
scorm.SetInteractionValue("cmi.interactions.7.correct_responses.0.pattern",
"1");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key7b0[i].checked)
{
question7 = document.getElementById("quizForm0").key7b0[i].value;
scorm.SetInteractionValue("cmi.interactions.7.student_response",question7);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.8.id","key8b0");
scorm.SetInteractionValue("cmi.interactions.8.type","choice");
scorm.SetInteractionValue("cmi.interactions.8.correct_responses.0.pattern",
"0");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key8b0[i].checked)
{
question8 = document.getElementById("quizForm0").key8b0[i].value;
scorm.SetInteractionValue("cmi.interactions.8.student_response",question8);
break;
}
}
scorm.SetInteractionValue("cmi.interactions.9.id","key9b0");
scorm.SetInteractionValue("cmi.interactions.9.type","choice");
scorm.SetInteractionValue("cmi.interactions.9.correct_responses.0.pattern",
"1");
for (var i=0; i < 2; i++)
{
if (document.getElementById("quizForm0").key9b0[i].checked)
{
question9 = document.getElementById("quizForm0").key9b0[i].value;
scorm.SetInteractionValue("cmi.interactions.9.student_response",question9);
break;
}
}
}
function calcRawScore(){
if (question0 == key0)
{
scorm.SetInteractionValue("cmi.interactions.0.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.0.result","wrong");
}
if (question1 == key1)
{
scorm.SetInteractionValue("cmi.interactions.1.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.1.result","wrong");
}
if (question2 == key2)
{
scorm.SetInteractionValue("cmi.interactions.2.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.2.result","wrong");
}
if (question3 == key3)
{
scorm.SetInteractionValue("cmi.interactions.3.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.3.result","wrong");
}
if (question4 == key4)
{
scorm.SetInteractionValue("cmi.interactions.4.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.4.result","wrong");
}
if (question5 == key5)
{
scorm.SetInteractionValue("cmi.interactions.5.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.5.result","wrong");
}
if (question6 == key6)
{
scorm.SetInteractionValue("cmi.interactions.6.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.6.result","wrong");
}
if (question7 == key7)
{
scorm.SetInteractionValue("cmi.interactions.7.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.7.result","wrong");
}
if (question8 == key8)
{
scorm.SetInteractionValue("cmi.interactions.8.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.8.result","wrong");
}
if (question9 == key9)
{
scorm.SetInteractionValue("cmi.interactions.9.result","correct");
rawScore++;
}
else
{
scorm.SetInteractionValue("cmi.interactions.9.result","wrong");
}
}
function calcScore2()
{
computeTime(); // the student has stopped here.
document.getElementById("quizForm0").submitB.disabled = true;
getAnswer();
calcRawScore();
actualScore = Math.round(rawScore / numQuestions * 100);
alert("Your score is " + actualScore + "%")
scorm.SetScoreRaw(actualScore+"" );
scorm.SetScoreMax("100");
var mode = scorm.GetMode();
if ( mode != "review" && mode != "browse" ){
if ( actualScore < 80 )
{
scorm.SetCompletionStatus("incomplete");
scorm.SetSuccessStatus("failed");
}
else
{
scorm.SetCompletionStatus("completed");
scorm.SetSuccessStatus("passed");
}
scorm.SetExit("");
}
exitPageStatus = true;
scorm.save();
scorm.quit();
}
//]]> -->
</script>
This is a very broad question and you will certainly need to learn JavaScript (or hire somebody) in order to solve this. I also recommend learning JQuery especially if you go with option 1 below. There's lots of sites that will teach you.
In terms of the general approach you can take, you have two choices:
Keep everything on one page and show/hide quiz questions as the learner progresses through.
Put questions in frames and pass data back to the parent frame.
I'd use option 1 myself because frames are quite old fashioned now.
Pick an approach, take some time to learn JavaScript and JQuery and then come back with more specific questions about problems you run into implementing the approach you choose.

Gridview search filter with paging

function Search_Gridview(strKey, strGV) {
var strData = strKey.value.toLowerCase().split(" ");
var tblData = document.getElementById(strGV);
var rowData;
for (var i = 1; i < tblData.rows.length; i++) {
rowData = tblData.rows[i].innerHTML;
var styleDisplay = 'none';
for (var j = 0; j < strData.length; j++) {
if (rowData.toLowerCase().indexOf(strData[j]) >= 0)
styleDisplay = '';
else {
styleDisplay = 'none';
break;
}
}
tblData.rows[i].style.display = styleDisplay;
}
}
i have a gridview and i want to add search filter on it. This code works correctly but search just one page not all rows. How i make this, i discovered but i didn't solve this.

avoid sorting in the JSP pages

var sortitems = 1;
function move(fbox, tbox, all)
{
for ( var i = 0; i < fbox.options.length; i++)
{
if (!all && fbox.options[i].selected && fbox.options[i].value != "")
{
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
}
else
{
if (all && fbox.options[i].value != "")
{
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
}
}
}
BumpUp(fbox);
if (sortitems)
SortD(tbox);
checkSelectAll();
}
This move function is getting called after clicking on the button, then it will call the sort method where sorting is happening by alphabetically. So we dont need to sort we need to populate the data as it is from the left side box to right side box and vice versa, but sorting is happening. Please help out be here.
function SortD(box)
{
var temp_opts = new Array();
var temp = new Object();
for ( var i = 0; i < box.options.length; i++)
{
temp_opts[i] = box.options[i];
}
for ( var x = 0; x < temp_opts.length - 1; x++)
{
for ( var y = (x + 1); y < temp_opts.length; y++)
{
if (temp_opts[x].value > temp_opts[y].value)
{
temp = temp_opts[x].text;
temp_opts[x].text = temp_opts[y].text;
temp_opts[y].text = temp;
temp = temp_opts[x].value;
temp_opts[x].value = temp_opts[y].value;
temp_opts[y].value = temp;
}
}
}
for ( var i = 0; i < box.options.length; i++)
{
box.options[i].value = temp_opts[i].value;
box.options[i].text = temp_opts[i].text;
}
}
Depends on the bumpup box function. The elements are moving from one box to another. It will replace the element with empty space and move to top and do for all the elements. Please help out me here
Thanks in advance
function BumpUp(box)
{
for ( var i = 0; i < box.options.length; i++)
{
if (box.options[i].value == "")
{
for ( var j = i; j < box.options.length - 1; j++)
{
box.options[j].value = box.options[j + 1].value;
box.options[j].text = box.options[j + 1].text;
}
var ln = i;
break;
}
}
if (ln < box.options.length)
{
box.options.length -= 1;
BumpUp(box);
}
}
Maybe it's just me, but it's hard to see what the issue is here.
If it is simply that SortD(tbox) is being called within the move() function, that's because
sortitems is set to 1 right at the top of the code. The value of sortitems is never changed anywhere else, so this conditional is always true and SortD is always called.
if (sortitems)
SortD(tbox);

Categories

Resources