HTML button action not working in Safari [closed] - javascript

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 9 years ago.
Improve this question
I'm running a web page in Safar, however; I notice that when I press the save button the event doesn't fire. It works fine in IE though. I've researched the issue, and found that it's not due to a missing value attribute or single/double quote specifics. Any help would be appreciated.
<input type='button' name='Save' id='saveCut' value='Save Cut' class=button onClick=\"Puma.saveTheCut()\">
JS function
Puma.saveTheCut = function () {
var offerId = inStoreCut.cutOfferFields[0];
var merchId = inStoreCut.cutOfferFields[1];
var adId = inStoreCut.cutOfferFields[2];
var eventID = inStoreCut.cutOfferFields[3];
var adNum = inStoreCut.cutOfferFields[4];
var cutID = inStoreCut.cutOfferFields[5];
var merchDescription = parent.main.document.getElementById('merchDescription').value;
var UPC = parent.main.document.getElementById('merchUPC').value;
var nrfSampleColorObj = parent.main.document.getElementById('itemColor');
var nrfSampleColor = nrfSampleColorObj.options[nrfSampleColorObj.selectedIndex].value;
var nrfsampleSubColorObj = parent.main.document.getElementById('itemChildColor');
var colorCodeStr = nrfsampleSubColorObj.options[nrfsampleSubColorObj.selectedIndex].value;
var nrfsampleSubColor = colorCodeStr.substring(0, 3);
var customer_Facing_Color = parent.main.document.getElementById('merchCustomerFacingColor').value;
var division = parent.main.document.getElementById('merchFob').value;
var deptNum = parent.main.document.getElementById('merchDept0~0').value;
var vendorNum = parent.main.document.getElementById('merchVendorNum0~0').value;
var pID = parent.main.document.getElementById('pID0~0').value;
var regPrice = parent.main.document.getElementById('regPrice').value;
var sampleSize = parent.main.document.getElementById('itemSize').value;
var itemQty = parent.main.document.getElementById('itemQty').value;
if (parent.main.document.getElementById('chkMerchSet').checked) {
var set = "1";
}
else {
var set = "0";
}
var sampleTypeObj = parent.main.document.getElementById("itemType");
var sampleType = sampleTypeObj.options[sampleTypeObj.selectedIndex].text;
var merchColorCorrObj = parent.main.document.getElementById("merchColorCorr");
var colorCorr = merchColorCorrObj.options[merchColorCorrObj.selectedIndex].value;
var merchSwatchObj = parent.main.document.getElementById("merchSwatch");
var Swatch = merchSwatchObj.options[merchSwatchObj.selectedIndex].value;
var pantoneColor = parent.main.document.getElementById('merchPantone').value;
var photoStylingDetails = parent.main.document.getElementById('merchPhotoStylingDetails').value;
var mCOMSampleId = parent.main.document.getElementById('mCOMSample').value;
var deptName = parent.main.document.getElementById('merchDeptName0~0').innerHTML;
var vendorName = Puma.decoder(parent.main.document.getElementById('merchVendorName0~0').innerHTML);
if (parent.main.document.getElementById('pidStatus0~0').value == "NOT IN PD") {
var pidStatus = "0";
}
else {
var pidStatus = "1";
}
var pidDescription = parent.main.document.getElementById('pidDescription').value;
var webId = parent.main.document.getElementById('webID0~0').innerHTML;
var vStyle = parent.main.document.getElementById('merchVStyle').value;
var markStyle = parent.main.document.getElementById('merchMarkStyle').value;
var subClass = parent.main.document.getElementById('Subclass').value;
// var productDescription = parent.main.document.getElementById('productDescription').value;
var docLineitemNum = parent.main.document.getElementById('merchDoc').value;
var merchTurnInStatusObj = parent.main.document.getElementById("merchTurnInStatus");
var turnInStatus = merchTurnInStatusObj.options[merchTurnInStatusObj.selectedIndex].text;
var reason = parent.main.document.getElementById('merchReason').value;
var merchCountryOriginObj = parent.main.document.getElementById("countryOfOrigin");
var countryOfOrigin = merchCountryOriginObj.options[merchCountryOriginObj.selectedIndex].value;
var importedCountry = parent.main.document.getElementById("importedCountries").value;
//var importedCountry = merchImportedCountryObj.options[merchImportedCountryObj.selectedIndex].text;
var fabricContent = parent.main.document.getElementById("fabricContent").value;
var careInstructions = parent.main.document.getElementById("careInstructions").value;
var offerDescription = parent.main.document.getElementById("offerDescription").value;
var onlyAtMacysObj = parent.main.document.getElementById("onlyAtMacys");
var onlyAtMacysValue = parseInt(onlyAtMacysObj.options[onlyAtMacysObj.selectedIndex].value, 10);
var onlyAtMacys = onlyAtMacysValue;
var legalOneObj = parent.main.document.getElementById("legalOne");
var legalOne = legalOneObj.options[legalOneObj.selectedIndex].value;
var legalOneExplain = parent.main.document.getElementById("explainLegalOne").value;
var legalTwoObj = parent.main.document.getElementById("legalTwo");
var legalTwo = legalTwoObj.options[legalTwoObj.selectedIndex].value;
var legalTwoExplain = parent.main.document.getElementById("explainLegalTwo").value;
var legalThreeObj = parent.main.document.getElementById("legalThree");
var legalThree = legalThreeObj.options[legalThreeObj.selectedIndex].value;
var legalThreeExplain = parent.main.document.getElementById("explainLegalThree").value;
var legalFourObj = parent.main.document.getElementById("legalFour");
var legalFour = legalFourObj.options[legalFourObj.selectedIndex].value;
var fiftyObj = parent.main.document.getElementById("overFifty");
var fifty = fiftyObj.options[fiftyObj.selectedIndex].value;
var userId = parent.botnav.uinfo.userID;
if (Puma.btiRequiredFieldIsValidated() == true) {
if (inStoreCut.existingRecord == false) {
sql = "action=saveMerchFormForCut&cutID=" + cutID +
//sql = "action=updateMerchFormForCut&cutID=" + cutID +
"&merchDescription=" + encodeURIComponent(merchDescription) +
"&UPC=" + UPC +
"&nrfSampleColor=" + nrfSampleColor +
"&nrfSampleSubColor=" + nrfsampleSubColor +
"&division=" + encodeURIComponent(division) +
"&deptNum=" + deptNum +
"&merchVendorNum=" + vendorNum +
"&pID=" + pID +
"&Customer_Facing_Color=" + encodeURIComponent(customer_Facing_Color) +
"&regPrice=" + regPrice +
"&sampleSize=" + encodeURIComponent(sampleSize) +
"&itemQty=" + itemQty +
"&set=" + set +
"&sampleType=" + sampleType +
"&colorCorr=" + colorCorr +
"&Swatch=" + Swatch +
"&pantoneColor=" + encodeURIComponent(pantoneColor) +
"&photoStylingDetails=" + encodeURIComponent(photoStylingDetails) +
"&mCOMSampleId=" + mCOMSampleId +
"&deptName=" + deptName +
"&vendorName=" + encodeURIComponent(vendorName) +
"&pidStatus=" + pidStatus +
"&pidDescription=" + pidDescription +
"&webId=" + webId +
"&vStyle=" + vStyle +
"&markStyle=" + markStyle +
"&subClass=" + subClass +
"&docLineItemNum=" + docLineitemNum +
"&merchTurnInStatus=" + turnInStatus +
"&reason=" + encodeURIComponent(reason) +
"&countryOfOrigin=" + countryOfOrigin +
"&importedCountry=" + importedCountry +
"&fabricContent=" + encodeURIComponent(fabricContent) +
"&careInstructions=" + encodeURIComponent(careInstructions) +
"&offerDescription=" + encodeURIComponent(offerDescription) +
"&onlyAtMacys=" + onlyAtMacys +
"&legalOne=" + legalOne +
"&legalOneExplain=" + legalOneExplain +
"&legalTwo=" + legalTwo +
"&legalTwoExplain=" + legalTwoExplain +
"&legalThree=" + legalThree +
"&legalThreeExplain=" + legalThree +
"&legalFour=" + legalFour +
"&fifty=" + fifty +
"&createdBy=" + userId
var ajaxMaster = new AjaxMaster(sql, "Puma.saveMerchFormForCutData(data)", "", "btiDispatcher.aspx");
sql = "action=updateMerchFormForCut&sql=" + encodeURIComponent(msql);
objAjaxAd.main_flag = "updateMerchFormForCut";
objAjaxAd.SendQuery(sql);
// "[t0].[signedByUserID]," +
// "[t0].[signedStatus]," +
//"[t0].[dateSigned]," +
//"[t0].[signedLastByUserID]," +
//"[t0].[dateLastSigned1]," +
//"[t0].[signedLastStatus]" +
// var ajaxMaster = new AjaxMaster(sql, "Puma.updateMerchFormForCutData(data)", "", "puma_core.aspx");
}
else {
sql = "action=updateMerchFormForCut&cutID=" + cutID +
"&offerId" + offerId +
"&merchId" + merchId +
"&adID" + adId +
"&eventID" + eventID +
"&adNum" + adNum +
"&merchDescription=" + encodeURIComponent(merchDescription) +
"&UPC=" + UPC +
"&nrfSampleColor=" + nrfSampleColor +
"&nrfSampleSubColor=" + nrfsampleSubColor +
"&division=" + encodeURIComponent(division) +
"&deptNum=" + deptNum +
"&merchVendorNum=" + vendorNum +
"&pID=" + pID +
"&Customer_Facing_Color=" + encodeURIComponent(customer_Facing_Color) +
"&regPrice=" + regPrice +
"&sampleSize=" + encodeURIComponent(sampleSize) +
"&itemQty=" + itemQty +
"&set=" + set +
"&sampleType=" + sampleType +
"&colorCorr=" + colorCorr +
"&Swatch=" + Swatch +
"&pantoneColor=" + encodeURIComponent(pantoneColor) +
"&photoStylingDetails=" + encodeURIComponent(photoStylingDetails) +
"&mCOMSampleId=" + mCOMSampleId +
"&deptName=" + deptName +
"&vendorName=" + encodeURIComponent(vendorName) +
"&pidStatus=" + pidStatus +
"&pidDescription=" + pidDescription +
"&webId=" + webId +
"&vStyle=" + vStyle +
"&markStyle=" + markStyle +
"&subClass=" + subClass +
"&docLineItemNum=" + docLineitemNum +
"&merchTurnInStatus=" + turnInStatus +
"&reason=" + encodeURIComponent(reason) +
"&countryOfOrigin=" + countryOfOrigin +
"&importedCountry=" + importedCountry +
"&fabricContent=" + encodeURIComponent(fabricContent) +
"&careInstructions=" + encodeURIComponent(careInstructions) +
"&offerDescription=" + encodeURIComponent(offerDescription) +
"&onlyAtMacys=" + onlyAtMacys +
"&legalOne=" + legalOne +
"&legalOneExplain=" + legalOneExplain +
"&legalTwo=" + legalTwo +
"&legalTwoExplain=" + legalTwoExplain +
"&legalThree=" + legalThree +
"&legalThreeExplain=" + legalThree +
"&legalFour=" + legalFour +
"&fifty=" + fifty +
//"&createdBy=" + userId
"&offerId=" + offerId +
"&merchId=" + merchId +
"&adID=" + adId +
"&eventID=" + eventID +
"&adNum=" + adNum
var ajaxMaster = new AjaxMaster(sql, "Puma.updateMerchFormForCutData(data)", "", "btiDispatcher.aspx");
sql = "action=updateMerchFormForCut&sql=" + encodeURIComponent(msql);
//objAjaxAd.SendQuery(sql);
// "[t0].[signedByUserID]," +
// "[t0].[signedStatus]," +
//"[t0].[dateSigned]," +
//"[t0].[signedLastByUserID]," +
//"[t0].[dateLastSigned1]," +
//"[t0].[signedLastStatus]" +
objAjaxAd.main_flag = "updateMerchFormForCutData";
objAjaxAd.SendQuery(sql);
//var ajaxMaster = new AjaxMaster(sql, "Puma.updateMerchFormForCutData(data)", "", "puma_core.aspx");
}
}
}

Write just " and not \" in the onClick attribute, or just omit the quotation marks:
onClick=Puma.saveTheCut()
The character \ has no special role in HTML; it’s just yet another character. So when you have onClick=\"Puma.saveTheCut()\", the actual attribute value is \"Puma.saveTheCut()\", which does not work of course, as you can see by looking at the console in the Developer Tools of your browser. You should see something the like following there:
SyntaxError: illegal character
\"Puma.saveTheCut()\"
(or with \"yup()\" when testing Agony’s jsfiddle).
As it is, the code should not work in any browser, and does not work in my IE 10 either.

Related

how to create a element to load the json data in a loop using only javascript

i want to display the theaters like a child node element of the demo node and after i want to trigger the onclick event for other function using only JavaScript
how to use foreach for creating html dom for each array in json data
i want it display like this
<div id="demo">
<div>theater in vizag</div>
<div>sangam : 4K,DolbyAtmos</div>
<div>sarat : 4K,DolbyAtmos</div>
<div>melody : 4K,DolbyAtmos</div>
</div>
let VizagData = '{"theater":[' +
'{"TheaterName":"sangam","Quality":"4K","sound":"DolbyAtmos" },' +
'{"TheaterName":"sarat","Quality":"4K","sound":"DolbyAtmos" },'+
'{"TheaterName":"melody","Quality":"4K","sound":"DolbyAtmos"}]}';
let VZMData = '{"theater":[' +
'{"TheaterName":"Srikanya","Quality":"4K","sound":"DolbyAtmos" },' +
'{"TheaterName":"kameswari","Quality":"4K","sound":"DolbyAtmos" },'+
'{"TheaterName":"IMAX","Quality":"4K","sound":"DolbyAtmos"}]}';
function search() {
document.getElementById('th').innerHTML = myfunction();
function myfunction() {
var value = document.getElementById('ct').value;
var th1 = document.createElement('div');
document.getElementById('th').appendChild(th1);
th1.innerHTML = "theaters in " + value;
return th1.innerHTML;
}
}
function data() {
var index = document.getElementById('ct');
var demo = document.getElementById("demo");
if(index.value === "vizag"){
const obj = JSON.parse(VizagData);
demo.innerHTML = obj.theater[0].TheaterName + " : " + obj.theater[0].Quality + "," + obj.theater[0].sound + "<br>" + obj.theater[1].TheaterName + " : " + obj.theater[1].Quality + "," + obj.theater[1].sound + "<br>" + obj.theater[2].TheaterName + " : " + obj.theater[2].Quality + "," + obj.theater[2].sound;
}
else {
const obj = JSON.parse(VZMData);
demo.innerHTML = obj.theater[0].TheaterName + " : " + obj.theater[0].Quality + "," + obj.theater[0].sound + "<br>" + obj.theater[1].TheaterName + " : " + obj.theater[1].Quality + "," + obj.theater[1].sound + "<br>" + obj.theater[2].TheaterName + " : " + obj.theater[2].Quality + "," + obj.theater[2].sound;
}
}
var go = document.getElementById('go');
addEventListener('click', search);
addEventListener('click', data);
<select id="ct">
<option value="vizianagaram">vizianagaram</option>
<option value="vizag">vizag</option>
</select>
<button id="go">GO</button>
<div class="th" id="th"></div>
<div id="demo"></div>
Iterate over the data and build the innerHTML. Something like:
let VizagData = '{"theater":[' +
'{"TheaterName":"sangam","Quality":"4K","sound":"DolbyAtmos" },' +
'{"TheaterName":"sarat","Quality":"4K","sound":"DolbyAtmos" },'+
'{"TheaterName":"melody","Quality":"4K","sound":"DolbyAtmos"}]}';
let VZMData = '{"theater":[' +
'{"TheaterName":"Srikanya","Quality":"4K","sound":"DolbyAtmos" },' +
'{"TheaterName":"kameswari","Quality":"4K","sound":"DolbyAtmos" },'+
'{"TheaterName":"IMAX","Quality":"4K","sound":"DolbyAtmos"}]}';
function search() {
document.getElementById('th').innerHTML = myfunction();
function myfunction() {
var value = document.getElementById('ct').value;
var th1 = document.createElement('div');
document.getElementById('th').appendChild(th1);
th1.innerHTML = "theaters in " + value;
return th1.innerHTML;
}
}
function data() {
var index = document.getElementById('ct');
var demo = document.getElementById("demo");
var theatres = JSON.parse(VZMData);
if(index.value === "vizag"){
theatres = JSON.parse(VizagData);
}
var innerHTML = "";
for (var obj of theatres.theater) {
innerHTML += "<div>" + obj.TheaterName + " : " + obj.Quality + "," + obj.sound + "</div>";
}
demo.innerHTML = innerHTML;
}
var go = document.getElementById('go');
addEventListener('click', search);
addEventListener('click', data);
<select id="ct">
<option value="vizianagaram">vizianagaram</option>
<option value="vizag">vizag</option>
</select>
<button id="go">GO</button>
<div class="th" id="th"></div>
<div id="demo"></div>

Google Ads scripts - how to do customised or long rolling date ranges

I'm using the following script to export Google Ads data from my account, but I am stuck with Google's pre-set date ranges and can't figure out how/if it's possible to jimmy these. Ideal date range would be year to date, with the report refreshing each day and adding on a fresh day's data, but would also be interested if we can get all time to work.
I'm a coding novice, so apologies in advance.
Script:
function main() {
var currentSetting = new Object();
currentSetting.ss = SPREADSHEET_URL;
// Read Settings Sheet
var settingsSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(SETTINGS_SHEET_NAME);
var rows = settingsSheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var numSettingsRows = numRows - 1;
var sortString = "";
var filters = new Array();
for(var i = 0; i < numRows; i++) {
var row = values[i];
var settingName = row[0];
var settingOperator = row[1];
var settingValue = row[2];
var dataType = row[3];
debug(settingName + " " + settingOperator + " " + settingValue);
if(settingName.toLowerCase().indexOf("report type") != -1) {
var reportType = settingValue;
} else if(settingName.toLowerCase().indexOf("date range") != -1) {
var dateRange = settingValue;
} else if(settingName.toLowerCase().indexOf("sort order") != -1) {
var sortDirection = dataType || "DESC";
if(settingValue) var sortString = "ORDER BY " + settingValue + " " + sortDirection;
var sortColumnIndex = 1;
}else {
if(settingOperator && settingValue) {
if(dataType.toLowerCase().indexOf("long") != -1 || dataType.toLowerCase().indexOf("double") != -1 || dataType.toLowerCase().indexOf("money") != -1 || dataType.toLowerCase().indexOf("integer") != -1) {
var filter = settingName + " " + settingOperator + " " + settingValue;
} else {
if(settingValue.indexOf("'") != -1) {
var filter = settingName + " " + settingOperator + ' "' + settingValue + '"';
} else if(settingValue.indexOf("'") != -1) {
var filter = settingName + " " + settingOperator + " '" + settingValue + "'";
} else {
var filter = settingName + " " + settingOperator + " '" + settingValue + "'";
}
}
debug("filter: " + filter)
filters.push(filter);
}
}
}
// Process the report sheet and fill in the data
var reportSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(REPORT_SHEET_NAME);
var rows = reportSheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var numSettingsRows = numRows - 1;
// Read Header Row and match names to settings
var headerNames = new Array();
var row = values[0];
for(var i = 0; i < numCols; i++) {
var value = row[i];
headerNames.push(value);
//debug(value);
}
if(reportType.toLowerCase().indexOf("performance") != -1) {
var dateString = ' DURING ' + dateRange;
} else {
var dateString = "";
}
if(filters.length) {
var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + ' WHERE ' + filters.join(" AND ") + dateString + " " + sortString;
} else {
var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + dateString + " " + sortString;
}
debug(query);
var report = AdWordsApp.report(query);
try {
report.exportToSheet(reportSheet);
var subject = "Your " + reportType + " for " + dateRange + " for " + AdWordsApp.currentAccount().getName() + " is ready";
var body = currentSetting.ss + "<br>You can now add this data to <a href='https://www.optmyzr.com'>Optmyzr</a> or another reporting system.";
MailApp.sendEmail(EMAIL_ADDRESSES, subject, body);
Logger.log("Your report is ready at " + currentSetting.ss);
Logger.log("You can include this in your scheduled Optmyzr reports or another reporting tool.");
} catch (e) {
debug("error: " + e);
}
}
function debug(text) {
if(DEBUG) Logger.log(text);
}
I've tried overwriting the data validation in the host spreadsheet, but think I need to amend the script itself also.

how to get value from select without refresh?

I made a CGPA-SGPA calculator and I cannot get the value with refresh.
When I refresh and check the value it get feed
but not getting value without refresh.
var credit_1001 = document.querySelector('#credit_1001')
var grade_1001 = document.querySelector('#grade_1001')
var credit_1002 = document.querySelector('#credit_1002')
var grade_1002 = document.querySelector('#grade_1002')
var credit_1003 = document.querySelector('#credit_1003')
var grade_1003 = document.querySelector('#grade_1003')
var credit_1004 = document.querySelector('#credit_1004')
var grade_1004 = document.querySelector('#grade_1004')
var credit_1008 = document.querySelector('#credit_1008')
var grade_1008 = document.querySelector('#grade_1008')
var credit_1009 = document.querySelector('#credit_1009')
var grade_1009 = document.querySelector('#grade_1009')
//btn and result S1
var btn_check = document.querySelector('#check_s1')
var result_s1 = document.querySelector('#result_s1')
//btn click S1
var total_s1_credit = parseInt(credit_1001.value) + parseInt(credit_1002.value) + parseInt(credit_1003.value) + parseInt(credit_1004.value) + parseInt(credit_1008.value) + parseInt(credit_1009.value)
var got_s1_marks = parseInt(credit_1001.value) * parseInt(grade_1001.value) + parseInt(credit_1002.value) * parseInt(grade_1002.value) + parseInt(credit_1003.value) * parseInt(grade_1003.value) + parseInt(credit_1004.value) * parseInt(grade_1004.value) + parseInt(credit_1008.value) * parseInt(grade_1008.value) + parseInt(credit_1009.value) * parseInt(grade_1009.value)
btn_check.addEventListener('click', () => {
result_s1.innerText = " S1 SGPA " + got_s1_marks / total_s1_credit
console.log("S1 Total Credit = " + total_s1_credit)
console.log("S1 Got Marks = " + got_s1_marks)
console.log("S1 SGPA = " + result_s1.innerText)
})
You need to get the values for your calculation inside of the click handler, right now you get them before the click, and then use (the old values) when the click happens
btn_check.addEventListener('click', () => {
//btn click S1
var total_s1_credit = parseInt(credit_1001.value) + parseInt(credit_1002.value) + parseInt(credit_1003.value) + parseInt(credit_1004.value) + parseInt(credit_1008.value) + parseInt(credit_1009.value)
var got_s1_marks = parseInt(credit_1001.value) * parseInt(grade_1001.value) + parseInt(credit_1002.value) * parseInt(grade_1002.value) + parseInt(credit_1003.value) * parseInt(grade_1003.value) + parseInt(credit_1004.value) * parseInt(grade_1004.value) + parseInt(credit_1008.value) * parseInt(grade_1008.value) + parseInt(credit_1009.value) * parseInt(grade_1009.value)
result_s1.innerText = " S1 SGPA " + got_s1_marks / total_s1_credit
console.log("S1 Total Credit = " + total_s1_credit)
console.log("S1 Got Marks = " + got_s1_marks)
console.log("S1 SGPA = " + result_s1.innerText)
})

Generate random equations with random numbers [closed]

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 generate random equations in JavaScriptp and then output them into an HTML tag.
This is the code:
<!DOCTYPE html>
<body>
<p>Click the button below to generate a random equation.</p>
<button onclick="change();">Generate</button>
<p id="generate"></p>
<script>
function getRandomizer(bottom, top) {
return function() {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
}
function getRandomNumber(results) {
var rollDie = getRandomizer( 1, 10 );
for ( var i = 0; i < 3; i++ ) {
results += rollDie() + "";
}
getRandomNumber.result = results;
}
function getRandomEquation(num1, num2, num3, num4, num5, num6, num7, output) {
var num_7,
num_6,
num_5,
num_4,
num_3,
num_2,
num_1
getRandomNumber(num1).result = num_7;
getRandomNumber(num2).result = num_6;
getRandomNumber(num3).result = num_5;
getRandomNumber(num4).result = num_4;
getRandomNumber(num5).result = num_3;
getRandomNumber(num6).result = num_2;
getRandomNumber(num7).result = num_1;
var equation1 = "" + num_1 + " x " + num_2 + " + {" + num_3 + " x [(" + num_4 + " x " + num_5 + ") - " + num_6 + "] + " + num_7 + "} = x",
equation2 = "" + num_1 + " x " + num_2 + " = y",
equation3 = "" + num_1 + "s x " + num_2 + " = z, s = " + num_3,
equation4 = "" + num_1 + " + {" + num_2 + " x [" + num_3 + " + (" + num_4 + " x " + num_5 + ") + " + num_6 + "] + " + num_7 + "} = x",
equation5 = "" + num_1 + "e + " + num_2 + "l x " + num_3 + " + " + num_4 + "a, e = " + num_5 + ", l = " + num_6 + ", a = " + num_7,
equation6 = "[" + num_1 + " x " + num_2 + "z] + {" + num_3 + " - " + num_4 + "} + (" + num_5 + " + " + num_6 + ") = e, z = " + num_7,
equation7 = "p" + " x " + num_1 + " / " + num_2 + " - " + num_3 + " + " + num_4 + " = e, p = " + num_5
var values = [
// there is an easier way to do this, too lazy
"" + equation1,
"" + equation2,
"" + equation3,
"" + equation4,
"" + equation5,
"" + equation6,
"" + equation7
]
var i = 0;
var e;
if (i > values.length) {
i = 0;
}
var randomEquation = values[i];
i++;
e = values[i];
this.output = randomEquation;
this.e = e;
}
function getEquation() {
var bl1,
bl2,
bl3,
bl4,
bl5,
bl6,
bl7,
equationOutput;
var eq = getRandomEquation(bl1, bl2, bl3, bl4, bl5, bl6, bl7, equationOutput).e;
getEquation.equation = eq;
}
function change() {
var final = getEquation().equation;
document.getElementById("generate").innerHTML = final;
}
</script>
</body>
</html>
But it dosen't work. Any help?
P.S. My teacher assigned this to me. Please respond as soon as possible. Thanks.
This code is a complete mess. I dont know where it comes from, but definitely not Javascript.
Try the following instead:
<!DOCTYPE html>
<body>
<p>Click the button below to generate a random equation.</p>
<button onclick="change();">Generate</button>
<p id="generate"></p>
<script>
function getRandomizer(bottom, top) {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
function getRandomNumber() {
var results="";
for ( var i = 0; i < 3; i++ ) {
results += getRandomizer( 1, 10 );
}
return results;
}
function getRandomEquation() {
var num_7 = getRandomNumber(),
num_6 = getRandomNumber(),
num_5 = getRandomNumber(),
num_4 = getRandomNumber(),
num_3 = getRandomNumber(),
num_2 = getRandomNumber(),
num_1 = getRandomNumber();
var equation1 = num_1+" x "+num_2+" + {"+num_3+" x [("+num_4+" x "+num_5+") - "+num_6+"] + "+num_7+"} = x",
equation2 = num_1+" x "+num_2+" = y",
equation3 = num_1+"s x "+num_2+" = z, s = "+num_3,
equation4 = num_1+" + {" +num_2+ " x [" +num_3+" + ("+num_4+" x "+num_5+") + "+num_6+"] + "+num_7+"} = x",
equation5 = num_1+"e + "+num_2+"l x "+num_3+" + "+num_4+"a, e = "+num_5+", l = "+num_6+", a = "+ num_7,
equation6 = "["+num_1+" x "+num_2+ "z] + {"+num_3+" - "+num_4+"} + ("+num_5+" + "+num_6+") = e, z = "+ num_7,
equation7 = "p x "+num_1+" / "+num_2+" - "+num_3+" + "+num_4+" = e, p = "+num_5
var randomEquation = [
equation1,
equation2,
equation3,
equation4,
equation5,
equation6,
equation7
]
return randomEquation.join("<br>");
}
function change() {
document.getElementById("generate").innerHTML = getRandomEquation();
}
</script>
</body>
</html>
Well, you could do this:
var type = (Math.floor(Math.random(4));
var ret = 0;
var n = [(Math.floor(Math.random(100)), (Math.floor(Math.random(100))];
if (type == 0) ret = n[0] + n[1]
else if (type == 1) ret = Math.abs(n[0] - n[1])
else if (type == 2) ret = n[0] * n[1];
else if (type == 3) ret = n[0] / n[1]
else ret = n[0] / 5 % n[1]
// do something with ret
It's fully expandable, just edit n and the if statements

Is it possible to re-do function, so it can be used many times with a different values

I have a function which I will be using many times, just with a different values.
$('.switch').click(function() {
var frontImg = $('#frontImg');
var frontImg1 = FrontImagePath + darkImageID + ".png";
var frontImg2 = FrontImagePath + frontImageID + ".png";
frontImg.attr('src', frontImg.attr('src') == frontImg1 ? frontImg2 : frontImg1);
var sideImg = $('#sideImg');
var sideImg1 = FrontImagePath + darkSideID + ".png";
var sideImg2 = FrontImagePath + sideImageID + ".png";
sideImg.attr('src', sideImg.attr('src') == sideImg1 ? sideImg2 : sideImg1);
});
Is it possible to re-do it/make it shorter, so I can use it for a different .click() event, by just changing var values.
Try this:
function addClick(e) {
e.click(function() {
var frontImg = $('#frontImg');
var frontImg1 = FrontImagePath + darkImageID + ".png";
var frontImg2 = FrontImagePath + frontImageID + ".png";
frontImg.attr('src', frontImg.attr('src') == frontImg1 ? frontImg2 : frontImg1);
var sideImg = $('#sideImg');
var sideImg1 = FrontImagePath + darkSideID + ".png";
var sideImg2 = FrontImagePath + sideImageID + ".png";
sideImg.attr('src', sideImg.attr('src') == sideImg1 ? sideImg2 : sideImg1);
});
}

Categories