My CoffeeScript code:
myloop = () ->
size = parseInt $('#size').val
$('#result').css 'text-align', 'center'
for i in [1..size] by 1
for j in [1..i] by 1
$('#result').append "<img src='alpha.jpg' />"
$('#result').append "<br />"
Compile into Javascript:
// Generated by CoffeeScript 1.6.3
(function() {
var myloop;
myloop = function() {
var i, j, size, _i, _j, _results;
size = parseInt($('#size').val);
$('#result').css('text-align', 'center')
_results = [];
for (i = _i = 1; _i <= size; i = _i += 1) {
for (j = _j = 1; _j <= i; j = _j += 1) {
$('#result').append("<img src='alpha.jpg' />");
}
_results.push($('#result').append("<br />"));
}
return _results;
};
}).call(this);
As my expect that _result should not be generated.
It should be $('#result').append("<br />") instead.
How can I fixed this? Thx.
Look at the docs:
Sometimes functions end with loops that are intended to run only for their side-effects. Be careful that you're not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value — like true — or null, to the bottom of your function.
To "fix" your code, just add a return statement at the end of your function.
Related
I'm trying to implement Zobrist hashing for a chess game and thought I was doing things correctly. However, when I try to apply XOR to a Hash value, it simply changes between 0 (the starting value for the hash) and -2147483648, in spite of conducting a bitwise XOR against a random number between 0 and 18446744073709551616.
Here's some code. I thought it was relatively simple with a struct for each entry in the Grid, a constructor function to make an entry, a function to create the Hash Table, and finally one for the Hash itself.
//Hash Table struct. This can be expanded for each new piece type
HashTableEntry = {
QueenHash:-1,
BishopHash:-1,
KnightHash:-1,
RookHash:-1,
PawnHash:-1,
KingHash:-1
};
//A function for creating a Hash Table Entry
function toHashTableEntryStruct() constructor {
var HashUpperBound = 18446744073709551616;
QueenHash = random(HashUpperBound);
BishopHash = random(HashUpperBound);
KnightHash = random(HashUpperBound);
RookHash = random(HashUpperBound);
PawnHash = random(HashUpperBound);
KingHash = random(HashUpperBound);
}
//A function to create a ds_grid Hash Table of the size passed to it, which it then returns
function CreateHashTable(_width, _height){
//Copy the local variables
var width = _width;
var height = _height;
//Create the grid for the Hash Table
var NewHashTable = ds_grid_create(width, height);
//Copy the original seed and set the game's seed to create the same table each time
var OriginalSeed = random_get_seed;
random_set_seed(280804);
//Loop through the Grid and fill each value with a HashTableEntry struct
for(var i = 0; i < width; i++;){
for(var j = 0; j < height; j++;){
var NewHash = new toHashTableEntryStruct();
ds_grid_set(NewHashTable, i, j, NewHash);
}
}
//Reset the seed
random_set_seed(OriginalSeed);
//Return the shiny new Hash Table
return NewHashTable;
}
//A function for creating the original Hash of a board. This should only be called on
initialising a Board as it sets the Original Hash
function CreateBoardHash(_board){
var Board = _board;
var width = ds_grid_width(Board);
var height = ds_grid_height(Board);
HashTable = CreateHashTable(width, height);
var FinalHash = 0;
for(var i = 0; i < width; i++;){
for(var j = 0; j < height; j++;){
var PieceToCheck = ds_grid_get(Board, i, j);
if(PieceToCheck == Pieces.BLANK){ continue; }
var HashEntry = ds_grid_get(HashTable, i, j);
var XorValue = MatchPieceToHashTableEntry(PieceToCheck, HashEntry);
FinalHash ^= XorValue;
}
}
if(FinalHash == 0){
//Error here
return;
}
//Set the instance variable for this original Hash
OriginalHash = FinalHash;
//Add it to the Current Hash stack
CurrentHash = FinalHash;
//Return said hash for the calling function
return FinalHash;
}
The problem comes in the FinalHash ^= XorValue; line, where XorValue each time is a random number, but FinalHash only ever comes out as 0 or -2147483648.
Can someone help with why this is the case and what I may be doing wrong with my XOR function?
Thanks to Reddit, I have fixed this. GameMaker must be doing something weird with the ^= operator, as the solution was as follows:
FinalHash = FinalHash ^ XorValue;
I want to first letter to be in upper case other in lower. But after ".", it must be upper again..
function firstToUpperCase( str ) {
return str.substr(0, 1).toUpperCase() + str.substr(1);
}
var str = 'prompt("Enter text to convert: ")
var Upcase = firstToUpperCase( str );
document.write(Upcase);
Here's a simplistic answer based on what you provided. It does not take whitespace into account following the period since you didn't mention that in the specs.
function firstToUpperCase(str) {
var parts = str.split(".");
for (i = 0; i < parts.length; i++) {
parts[i] = parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1).toLowerCase();
}
return parts.join(".");
}
If you're trying to deal with sentences, something like this might be a little better, though it does not preserve exact whitespace:
function firstToUpperCase(str) {
var parts = str.split(".");
for (i = 0; i < parts.length; i++) {
sentence = parts[i].trim();
parts[i] = sentence.substring(0, 1).toUpperCase() + sentence.substring(1).toLowerCase();
}
return parts.join(". ");
I've got this Coffeescript here:
brew = (args...) =>
for e in args
alert e
null
brew('fo', 're', 'eo');
I wish I didn't need to put null there to get it to work, but alas, that compiles to this:
brew = function() {
var args, e, _i, _len, _results;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
_results = [];
for (_i = 0, _len = args.length; _i < _len; _i++) {
e = args[_i];
alert(e);
_results.push(null);
}
return _results;
};
brew('fo', 're', 'eo');
But now I have 3 unnecessary lines:
_results = [];
_results.push(null);
return _results;
Any tips?
If you don't want a function to return anything, say so:
brew = (args...) =>
for e in args
console.log e
return
A side effect of that is that the for loop won't populate an array: CoffeeScript can guarantee that the result of the for loop expression won't be used so it won't bother calculating it. Keep in mind that everything is an expression in CoffeeScript and functions return the value of their last expression so sometimes you have to throw in explicit returns to avoid wasting time computing things that will never get used.
That CoffeeScript loop ends up like this:
for (_i = 0, _len = args.length; _i < _len; _i++) {
e = args[_i];
console.log(e);
}
Note that the explicit "return nothing" return suppresses all the _result stuff.
You can see it yourself over here.
What about this
brew = (args...) -> args.forEach alert
which compiles to
var brew,
__slice = [].slice;
brew = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return args.forEach(alert);
};
brew('fo', 're', 'eo');
I have the following code that obviously works but I am fairly sure there is a more terse way of expressing this in coffeescript:
todos = []
for i in [1..10]
todos.push App.store.find App.Todo, i
todos = (App.store.find(App.Todo, i) for i in [1..10])
The enclosing parentheses indicate a list comprehension, which collects the return values into an array and returns it.
Consider the two following examples. The enclosing parentheses change how Coffeescript interprets the loop.
# With parentheses (list comprehension)
todos = (App.store.find(App.Todo, i) for i in [1..10])
# Without parentheses (plain old loop)
todos = App.store.find(App.Todo, i) for i in [1..10]
And the output:
// With parentheses
todos = (function() {
var _i, _results;
_results = [];
for (i = _i = 1; _i <= 10; i = ++_i) {
_results.push(App.store.find(App.Todo, i));
}
return _results;
})();
// Without parentheses
for (i = _i = 1; _i <= 10; i = ++_i) {
todos = App.store.find(App.Todo, i);
}
I'm translating a piece of code from javascript to coffeescript.
for (var i = 0, len = keys.length; i < len; i++) {
k = keys[i];
if (!mailOptions.hasOwnProperty(k))
mailOptions[k] = app.set('mailOptions')[k]
}
I have no idea how to approach to it and on the doc website is not clear, can someone give me a clear explanation? thank you very much!
for key in keys
if not mailOptions.hasOwnProperty key
mailOptions[key] = (app.set 'mailOptions')[key]
Or guard-style:
for key in keys when not mailOptions.hasOwnProperty key
mailOptions[key] = (app.set 'mailOptions')[key]
Compiles to:
var key, _i, _len;
for (_i = 0, _len = keys.length; _i < _len; _i++) {
key = keys[_i];
if (!mailOptions.hasOwnProperty(key)) {
mailOptions[key] = (app.set('mailOptions'))[key];
}
}
Here's one way (from here: http://js2coffee.org/):
i = 0
len = keys.length
while i < len
k = keys[i]
mailOptions[k] = app.set("mailOptions")[k] unless mailOptions.hasOwnProperty(k)
i++
But I wouldn't do it this way. I would just do:
for k in keys
mailOptions[k] = app.set("mailOptions")[k] unless mailOptions.hasOwnProperty k
This outputs the following (excluding var, which it also outputs):
for (_i = 0, _len = keys.length; _i < _len; _i++) {
k = keys[_i];
if (!mailOptions.hasOwnProperty(k)) {
mailOptions[k] = app.set("mailOptions")[k];
}
}
Or, if you wanted to be fancier, which I don't advise in this situation, since it sacrifices some readability:
(mailOptions[k] = app.set("mailOptions")[k] unless mailOptions.hasOwnProperty k) for k in keys