I am having trouble calling an arguement in the functin below.
function makeScales(size,note1,note2,note3,note4,note5,note6,note7,note8){
for(var z=0; z<size; z++){
if(semis=="Sharps"){
notes = notes + " - " + noteSharp["note" + z];
} else {
notes = notes + " - " + noteFlat["note" + z];
}
}
}
I went through many debugging procedures and found that my error en-lies with noteSharp["note" + z] more specifically "note" + z . For example if i do console.log(noteSharp[note1]) i get the desired result and i set z to 7 so i can see that its populating appropriately but I get undefined for my results. Any help would be greatly appreciated.
"note" + z will give you a string like "note1" not an identifier.
If you want to access a piece of data by an index, then put it in an array, not in a variable with a number in its name.
For example:
function makeScales(size, notes) {
//...
notes = notes + " = " + noteSharp[ notes[z] ];
//...
}
makeScales( "some size", [ "foo", "bar", "baz" ] );
Alternatively (and I wouldn't recommend this approach as it mixes different kinds of data in a single data structure), you can use the arguments object.
notes = notes + " - " + arguments[z];
arguments represents all the arguments passed to the function. The second argument (aka note1) will have the index 1 and so on.
Your problem there is that noteSharp[note1] is using the value of the variable note1 as the key to lookup, whereas noteSharp["note" + z] results in a string of "note" followed by the value of z.
To reference a variable name via a string, use eval:
noteSharp[eval("note" + z)];
However, I would highly recommend not using eval. Others do too, but perhaps there is a counterpoint.
Please do this instead, otherwise Cthulhu might arise...
Pass note1, note2, ..., noteN as properties of an object. Therefore you can look up their values exactly as you desire, just one level deeper.
function makeScales(size, notes) {
var z = 1;
var note1 = notes["note" + z];
}
// Calling makeScales with our notes.
makeScales(10, {
note1: 'c',
note2: 'd',
note3: 'e',
// etc.
});
Related
I have declared a simple array in my JavaScript and I'm trying to push values from another array that has a dictionary inside it. But only the first value is getting pushed and not the rest of them.
<script>
complist = []
var testjs = [{'issuancedte': 'Finance', 'totalcomp': 1}, {'issuancedte': 'AnotherOne', 'totalcomp': 5}]
for (opt in testjs)
if ((adm_section_array.includes(testjs[opt].issuancedte)))
$('#data').append('<tr><td>' + testjs[opt].issuancedte + '</td><td>' + testjs[opt].totalcomp + '</td></tr>')
complist.push(testjs[opt].totalcomp);
</script>
So, from the code above I should be getting:
complist = [1, 5]
but instead I'm only getting:
complist = [1]
For some completely unknown reasons, if I place the .push line above the one where I'm appending data to a form, the complist is made as it should be but the table doesn't get appended.
This should be written like this,
if ((adm_section_array.includes(testjs[opt].issuancedte))) {
$('#data').append('<tr><td>' + testjs[opt].issuancedte + '</td><td>' + testjs[opt].totalcomp + '</td></tr>')
complist.push(testjs[opt].totalcomp);
}
Notice the curly braces after if block.
I created a table called "morning" in AppLab, and one column stores data as an array (or list as it calls it). I'm able to properly add data to this array, but my problem is reading the data back (as I want to display it as a label/normal text on another page) If the numbers 1234 and 5678 are the values in the array, when I try to do
console.log(records[i].id + ': ' + records[i].buses);
The second value (buses) is the name of the column I'm trying to read back, which will result in "," rather than "1234,5678" and I'm not really sure what to do. This is the code I have so far, any help would be greatly appreciated!
readRecords("morning", {}, function(records) {
for (var i =0; i < records.length; i++) {
console.log((records[i]).id + ': ' + records[i].(buses[i]));
}
});
var ts1Buses = ["1234"];
var ts1Change;
onEvent("enterTS1", "click", function(event) {
appendItem(ts1Buses, getText("textTS1"));
updateRecord("morning", {id:1, buses:ts1Buses}, function(record, success) {
setText("textTS1", "");
});
});
The console.log statement in your longer block of code doesn't look quite right. try console.log(records[i].id + ': ' + records[i].buses); instead. if that doesn't work, please post a link to your project so that others can try to find a fix by remixing and editing it.
App Lab's data tables do not support arrays. They will have to be converted into comma-separated strings before creating or updating and converted to an array after reading.
To convert an array to a string, simply use the toString() method:
var array = ["a", "b", "c"];
console.log(array.toString()) // "a,b,c"
To convert a string into an array, use the split() method:
var string = "a,b,c";
console.log(string.split(","); // ["a", "b", "c"]
Using Testcomplete (javascript) for our automation.
I have created a function:
function SelectDropdownBoxItem(object, property, item)
{
var dropDown = eval(object + "." + FindChild(property, item, 5));
dropDown.Click();
}
Also tried without using eval...
When I call the method using something like this:
var AutoAddressSuggestionList = Aliases.b.pageGuidewireClaimc.panelBoundlist.AddressSuggestionList;
SelectDropdownBoxItem(AutoAddressSuggestionList,"contentText","1 Something Street*");
I get an error "Object Expected"... I have no idea why, because when I run this method without parameterizing it everything works.
Any ideas?
No need for eval here; you can call the method directly on the object:
var dropDown = object.FindChild(property, item, 5);
Also, it's a good idea to check that the list item was actually found:
if (dropDown.Exists) {
dropDown.Click();
}
else {
Log.Error(
"Drop-down list item was not found.",
"Object: " + object.FullName + "\r\n" +
"Item : " + item
);
}
I have an array of arrays declared as the following:
var rangeValues = [[0.001, 0.01], [0.0000001, 0.000001]];
This array is used to fill the values of a drop down, since I essentially need a tuple for each drop down item's value. I proceed to access the values of the drop down with the following:
rangeTuple = document.getElementById('rangeSelection').value;
console.log(rangeTuple);
selectedMinRange = rangeTuple[0];
selectedMaxRange = rangeTuple[1];
console.log(selectedMinRange + " | " + selectedMaxRange[1]);
And I receive the following output:
0.001,0.01
0 | .
In my understanding (albeit limited with JS :) ), rangeTuple should be an array with two items in it. When rangeTuple is logged, it looks correct. However, when I try and assign the items in this tuple to a pair global variables, the values are not the ones I expect.
Any help is appreciated,
The value of an input is always a string. You will need to split that string to use it in the way you want:
rangeTuple = document.getElementById('rangeSelection').value;
console.log(rangeTuple);
// split it
rangeTuple = rangeTuple.split(",");
selectedMinRange = rangeTuple[0];
selectedMaxRange = rangeTuple[1];
console.log(selectedMinRange + " | " + selectedMaxRange);
I often need to monitor the contents of variables when testing programs like this:
var anObject = {aProperty:true}; // this is just an example.
console.log('anObject.aProperty: ' + anObject.aProperty); <-- typed it twice.
I type the name of the variable into a string followed by typing the same thing again to reference the value.
It seems like an unnecessary duplication to write the same thing twice every time. Is there a way to do this by having to only write the name once using a function?
For example:
function show(value) {
console.log("'" + ??? + "':" + value):
}
so it can be used like this (or something similar):
show(anObject.aProperty);
The above is just a simple example. Basically what I'm asking is whether there is a way to get the name of the variable that was passed into a function so that the name can then be output as part of a string showing the value of the variable.
Haters are gonna hate:
http://jsfiddle.net/coma/6HTnB/
var anObject = {
aProperty: ['uno', 'dos', 'tres']
};
var log = function(object, property) {
var evil = 'object.' + property;
console.log(evil, eval(evil));
};
log(anObject, 'aProperty[2]');
Or even worse:
http://jsfiddle.net/coma/6HTnB/2/
var anObject = {
aProperty: ['uno', 'dos', 'tres']
};
var show = function(a) {
console.log(a + ':', eval(a));
};
show('anObject.aProperty[2]');
Well, eval is not evil per se, but the second approach is kind of ugly since the function needs to be in the correct scope.
Here is how I would write such a show() function:
function show(object, property) {
console.log(property + ":", object[property]);
}
To use it:
var mouse = { x: 100, y: 200 };
show(mouse, 'x');
And if you want to test it: http://jsfiddle.net/IQAndreas/c9SUm/
You don't print out the name of the object (and due to the way JavaScript variables and references work, there is no easy way to do so), you only get the name of the property you want to access. If you want the name of the object as well, you could manually print it out before listing the properties:
var mouse = { x: 100, y: 200 };
console.log("== mouse ==");
show(mouse, 'x');
show(mouse, 'y');
Which outputs:
"== mouse =="
"x:" 100
"y:" 200
You can however print the type of the object, (if you are using JavaScript's class features that is, otherwise, anything created with {} is just said to be an Object):
function show(object, property) {
var className = object.constructor.name;
console.log(className + "#" + property + ":", object[property]);
}
Sample output (assuming you have created the Mouse class):
"Mouse#x:" 100
"Mouse#y:" 200
And if you want to test it: http://jsfiddle.net/IQAndreas/c9SUm/2/
Based on coma's answer, I think this may be the solution:
function show() {
return function (value) {
console.log(value + ':', eval(value));
};
};
function aTest() {
obj = {x:1, y:2}; show()('obj');
};
function bTest() {
obj = {x:3, y:4}; show()('obj');
};
aTest(); // obj: { x: 1, y: 2 }
bTest(); // obj: { x: 3, y: 4 }