function show_alert(){
var month = oMonthList.value;
var day = oDayField.value;
var gametype = oGameTypeList.value;
var gamenum = oGameNumberField.value;
var gamename = oGameNameField.value;
var modname = oModNameField.value;
var phase = oPhaseList.value;
var phasenum = oPhaseNumberField.value;
var pagenum = oNameNumberField.value;
var repname = oReplacementNameField.value;
var modlink = oModLinkField.value;
alert(phase);
}
Why does this not show the alert when the function is called, but removing all variables except the one in question (var phase) does? I'm guessing it's something to do with syntax, but I cannot pin down the issue.
Did you make sure that your javascript code doesn't throw any exception? If some object is undeclared or undefined, the code may be aborted early thus alert() is not not executed.
Related
Hello I am new to the community and I am a novice coder with very little coding experience. I understand some basics and 1st part of the code is working. I am having a problem with the data.foreach(funtion(row) where it is giving a error with brackets and colons
function myFunction() {
var Name = 1;
var Surname = 2;
var AffilliateID = 24;
var emailTemp = HtmlService.createHtmlOutputFromFile("Affiliate email");
}
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Affiliate Responses");
var data = ws.getDataRange("A4:Y" + ws.getLastRow()).getDisplayValues();
data.forEach(function(row) **(**
emailTemp.Name = row[Name]**;**
emailTemp.Surname = row[Surname];
emailTemp.AffilliateID = row[AffiliateID];
))
I have created a var for each line and the tutorial I am following expresses the code as is above. The tutorial may be outdated and some help with an explanation would be appreciated. The bold is the errors.
Thanks
Glenn
For your loop it's something like that
data.forEach(row => {
emailTemp.Name = row[Name]
emailTemp.Surname = row[Surname];
emailTemp.AffilliateID = row[AffiliateID];
})
But to use your
var Name = 1;
var Surname = 2;
var AffilliateID = 24;
var emailTemp = HtmlService.createHtmlOutputFromFile("Affiliate email");
You have to be in the same scope
you need to declare correctly the function that you are passing as a parameter to forEach
const data = [2,5,1,3,4]
data.forEach(function myFunction(item){
console.log(item)
})
you can also use arrow functions:
const data = [2,5,1,3,4]
data.forEach(item => console.log(item))
Welcome to the community. This seems like a simple syntax issue.
You're using ( & ) brackets for function braces, when infact they should be { & } (like your first function).
It's also important that your variables in the right scope. You cannot access the emailTemp variable as it is scoped to your myFunction function. I've moved this into the global scope for you.
Your updated code would look something like this:
function myFunction() {
var Name = 1;
var Surname = 2;
var AffilliateID = 24;
}
var emailTemp = HtmlService.createHtmlOutputFromFile("Affiliate email");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Affiliate Responses");
var data = ws.getDataRange("A4:Y" + ws.getLastRow()).getDisplayValues();
data.forEach(function(row) {
emailTemp.Name = row[Name];
emailTemp.Surname = row[Surname];
emailTemp.AffilliateID = row[AffiliateID];
});
I'm trying to make a project that searches through a block of text, then pushes certain values to the properties of an object, but whenever I put a variable inside of the ingamePrices object at near the bottom of this block of text,
var testPrompt = prompt("Let's figure out how this works");
var rawUSDValue = 0.125;
function item(craftGamePrice, craftMarketPrice, uncraftGamePrice, uncraftMarketPrice, strangeGamePrice, strangeMarketPrice, genuineGamePrice, genuineMarketPrice, vintageGamePrice, vintageMarketPrice, unusualGamePrice, unusualMarketPrice, hauntedGamePrice, hauntedMarketPrice, collectorGamePrice, collectorMarketPrice )
{
this.craftGamePrice = craftGamePrice,
this.craftMarketPrice = craftMarketPrice,
this.uncraftGamePrice = uncraftGamePrice,
this.uncraftMarketPrice = uncraftMarketPrice,
this.strangeGamePrice = strangeGamePrice,
this.strangeMarketPrice = strangeMarketPrice,
this.genuineGamePrice = genuineGamePrice,
this.genuineMarketPrice = genuineMarketPrice,
this.vintageGamePrice = vintageGamePrice,
this.vintageMarketPrice = vintageMarketPrice,
this.unusualGamePrice = unusualGamePrice,
this.unusualMarketPrice = unusualMarketPrice,
this.hauntedGamePrice = hauntedGamePrice,
this.hauntedMarketPrice = hauntedMarketPrice,
this.collectorGamePrice = collectorGamePrice,
this.collectorMarketPrice = collectorMarketPrice
}
var ingamePrices =
{
};
document.write(testPrompt);
so that it's like this
var testPrompt = prompt("Let's figure out how this works");
var rawUSDValue = 0.125;
function item(craftGamePrice, craftMarketPrice, uncraftGamePrice, uncraftMarketPrice, strangeGamePrice, strangeMarketPrice, genuineGamePrice, genuineMarketPrice, vintageGamePrice, vintageMarketPrice, unusualGamePrice, unusualMarketPrice, hauntedGamePrice, hauntedMarketPrice, collectorGamePrice, collectorMarketPrice )
{
this.craftGamePrice = craftGamePrice,
this.craftMarketPrice = craftMarketPrice,
this.uncraftGamePrice = uncraftGamePrice,
this.uncraftMarketPrice = uncraftMarketPrice,
this.strangeGamePrice = strangeGamePrice,
this.strangeMarketPrice = strangeMarketPrice,
this.genuineGamePrice = genuineGamePrice,
this.genuineMarketPrice = genuineMarketPrice,
this.vintageGamePrice = vintageGamePrice,
this.vintageMarketPrice = vintageMarketPrice,
this.unusualGamePrice = unusualGamePrice,
this.unusualMarketPrice = unusualMarketPrice,
this.hauntedGamePrice = hauntedGamePrice,
this.hauntedMarketPrice = hauntedMarketPrice,
this.collectorGamePrice = collectorGamePrice,
this.collectorMarketPrice = collectorMarketPrice
}
var ingamePrices =
{
var testVariable = "sampleString";
};
document.write(testPrompt);
it causes the "prompt" command to stop working. Does anyone know why, or how to fix it?
var ingamePrices =
{
var testVariable = "sampleString";
};
This might be an attempt at one of two things: an object literal, or block syntax which you imagine will contain testVariable. Object literals contain keys and values, they don't contain arbitrary expressions or variable definitions. As an object literal this should be
var ingamePrices =
{
testVariable: "sampleString"
};
Or possibly, if you really did want a testVariable as context for some of the contents of this object, then:
var testVariable = "sampleString",
ingamePrices =
{
blah: [testVariable, "a use of testVariable"]
};
If you were looking for block syntax, and lexical variables, then JavaScript doesn't have them. It only has global and function variables. Which means cases like this become a self-executing function, purely to provide scope:
var ingamePrices = (function() {
var testVariable = "sampleString";
...
return { blah: testVariable };
})()
The program is supposed to be a live search using php and javascript... where as you type it searches below. I just recently started learning javascript so sorry if my knowledge is limited...
$(document).ready(function () {
$('#results').append('<p>Started</p>');
var getText = (function () {
return document.getElementById('year').value;
});
var text = getText;
var getText1 = (function () {
return document.getElementById('class').value;
});
var text1 = getText1;
setInterval(function () {
var newText = getText;
var newText1 = getText1;
var loading = "search.php?year=" + newText + "&class=" + newText1;
$('#results').append(newText1);
if (text !== newText || text1 !== newText1) {
$('#results').load(loading);
$('#results').append('somethinghappened');
};
text = newText;
text1 = newText1;
}, 100);
});
so it works fine when i append newText1, however if i try to append "loading" it returns:
search.php?year=function () { return document.getElementById("year").value; }&class=function () { return document.getElementById("class").value; }
Can anyone explain what the difference between the two cases is and why a difference occurs? and possibly how to fix it so that it loads the correct URL
i searched and found: JavaScript: When assigning an anonymous function to a variable, function return value is not passed, rather the function as a string however didn't quite understand completely what it meant by passing two arguments, and when i tried to do something similar, it didn't work as expected...
Any help is appreciated and thanks in advance.
var newText = getText;
var newText1 = getText1;
You are assigning the functions getText and getText1 to the variables instead of executing them and assigning their return values. Try this instead:
var newText = getText();
var newText1 = getText1();
Or just:
var newText = document.getElementById('year').value;
var newText1 = document.getElementById('class').value;
try simply with
var getText = document.getElementById('year').value;
var getText1 = document.getElementById('class').value;
otherwise you will have only a reference to the function
You're not calling the functions, you're just referencing them.
To call them you need parens, like getText(). But why make them functions in this case? They're short and don't take parameters. If you want functions, make a single function that takes the string ID.
I'm trying to translate a PHP class into JavaScript. The only thing I'm having trouble with is getting an item out of an array variable. I've created a simple jsfiddle here. I cannot figure out why it won't work.
(EDIT: I updated this code to better reflect what I'm doing. Sorry for the previous mistake.)
function tattooEightBall() {
this.subjects = ['a bear', 'a tiger', 'a sailor'];
this.prediction = make_prediction();
var that = this;
function array_random_pick(somearray) {
//return array[array_rand(array)];
var length = somearray.length;
var random = somearray[Math.floor(Math.random()*somearray.length)];
return random;
}
function make_prediction() {
var prediction = array_random_pick(this.subjects);
return prediction;
}
}
var test = tattooEightBall();
document.write(test.prediction);
Works fine here, you are simple not calling
classname();
After you define the function.
Update
When you make a call to *make_prediction* , this will not be in scope. You are right on the money creating a that variable, use it on *make_prediction* :
var that = this;
this.prediction = make_prediction();
function make_prediction() {
var prediction = ''; //initialize it
prediction = prediction + array_random_pick(that.subjects);
return prediction;
}
You can see a working version here: http://jsfiddle.net/zKcpC/
This is actually pretty complex and I believe someone with more experience in Javascript may be able to clarify the situation.
Edit2: Douglas Crockfords explains it with these words:
By convention, we make a private that variable. This is used to make
the object available to the private methods. This is a workaround for
an error in the ECMAScript Language Specification which causes this to
be set incorrectly for inner functions.
To see the complete article head to: http://javascript.crockford.com/private.html
You never call classname. Seems to be working fine.
Works for me:
(function classname() {
this.list = [];
this.list[0] = "tiger";
this.list[1] = "lion";
this.list[2] = "bear";
function pickone(somearray) {
var length = somearray.length;
var random = somearray[Math.floor(Math.random()*length)];
return random;
}
var random_item = pickone(this.list);
document.write(random_item);
}());
Were you actually calling the classname function? Note I wrapped your code block in:
([your_code]());
I'm not sure what you're trying to accomplish exactly with the class structure you were using so I made some guesses, but this code works by creating a classname object that has instance data and a pickone method:
function classname() {
this.list = [];
this.list[0] = "tiger";
this.list[1] = "lion";
this.list[2] = "bear";
this.pickone = function() {
var length = this.list.length;
var random = this.list[Math.floor(Math.random()*length)];
return random;
}
}
var cls = new classname();
var random = cls.pickone();
You can play with it interactively here: http://jsfiddle.net/jfriend00/ReL2h/.
It's working fine for me: http://jsfiddle.net/YznSE/6/ You just didn't call classname(). If you don't call it, nothing will happen ;)
Make it into a self-executing function like this:
(function classname() {
this.list = [];
this.list[0] = "tiger";
this.list[1] = "lion";
this.list[2] = "bear";
function pickone(somearray) {
var length = somearray.length; //<---WHY ISN'T THIS DEFINED??
var random = somearray[Math.floor(Math.random() * length)];
return random;
}
var random_item = pickone(this.list);
document.write(random_item);
})();
var test = tattooEightBall();
document.write(test.prediction);
Should be:
var test = new tattooEightBall(); //forgot new keyword to create object
document.write(test.prediction()); // forgot parens to fire method
and:
this.prediction = make_prediction();
Should be:
this.prediction = make_prediction;
This is the code that I made that has worked but all of a sudden stopped working:
var waitEqua = 1 * 1 * 1000;
function getTC() {
$.get (
'http://www.roblox.com/marketplace/tradecurrency.aspx',
function parseData(data) {
var stuff = $(data).find('.CurrencyQuote');
var rowh = stuff.find('.TableRow');
var rate = rowh.find('.Rate');
var rateb = /(......)(.)(......)/(rate.text());
var spread = rowh.find('.Spread').text();
localStorage["Tix"] = rateb[1];
localStorage["Robux"] = rateb[3];
localStorage["Spread"] = spread;
spreadTehToast(spread);
}
);
}
My error is at var rateb = /(......)(.)(......)/(rate.text()); with the error Uncaught TypeError: object is not a function. I have not changed the code. It has just broke.
var rateb = /(......)(.)(......)/(rate.text());
is not valid JS to the best of my knowledge (the RegEx is not a function but an object as the error suggests, yet you're trying to use it as a function), it looks like a call to exec() has gone missing. Try this:
var rateb = /(......)(.)(......)/.exec(rate.text());