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());
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];
});
Here is what my index.html looks like:
And this is the Javascript code (angular):
var controllerElement = document.querySelector('[id="tile3"]');
console.log(controllerElement.getAttribute('class'));
var controllerScope = angular.element(controllerElement).scope();
As you can see, I'm trying to find the controllerElement by searching for an id equal to tile3. However, whenever I get to the next line the program crashes with this error:
Uncaught TypeError: Cannot read property 'getAttribute' of null
Is there something obvious I'm missing?
EDIT
Here is the full code, now the controllerScope var is being undefined for some reason...
var update = function(id, channel){
var controllerElement = document.querySelector('#tile3');
console.log(controllerElement.getAttribute('ng-controller'));
var controllerScope = angular.element(controllerElement).scope();
console.log(controllerScope);
controllerScope.$apply(function () {
controllerScope[id].username = channel.username;
controllerScope[id].keyword = channel.keyword;
controllerScope[id].percent = channel.percent;
controllerScope[id].views = channel.views;
controllerScope[id].link = channel.link;
});
};
(function(){
var app = angular.module("testApp", []);
var TileController = function($scope){
$scope.channels = [];
for(i = 0; i < 25; i++){
var channel = {
username:"John",
keyword:"Doe",
percent:"50%",
views:5000,
link:"http://www.twitch.tv/lolyou"
};
$scope.channels.push(channel);
}
};
app.controller("TileController", ["$scope", TileController]);
update(3, {username:"Yo",
keyword:"Bro",
percent:"40%",
views:35,
link:"http://www.twitch.tv/nickbunyun"});
})();
The line where it says console.log(controllerScope); is just printing "undefined".
If you are using querySelector then you could/should just use #tile3 as value passed to the function, so:
var tile3 = document.querySelector('#tile3')
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;
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.
Why doesn't this return a map I can iterate over?
function createSObjectMap()
{
//this = {}; //AssociativeArray();
this["divEle"] = -1;
this["ID"] = "-1";
this["elID"] = "-1";
}
var sObj = new createSObjectMap();
// The follow crashes
// Error: "TypeError: Doesn't support this action"
for (var x in sObj)
{
alert( x+": "+sObj[s] );
}
Your code works fine.
However, you wrote s instead of x.
Because your loop variable is x and you're referencing sObj[s].