I’m getting some strange results for a script I’m writing to for a typewriter app. just like when using a typewriter, there are only so many characters you can type on a single line before you need to hit RETURN. I’m trying to determine how many (fixed width) letters have been typed, and check that number against a variable line length (variable because, just like when using a typewriter, you might not begin typing at the left-most margin).
Here are my global variables:
var charLine = "Hello world"; // what has been typed
var paperWidth = 900; // width of "paper" in the typewriter
var strikePosition = 488; // where the type strikes the paper (known value, the center of the typewriter)
var charWidth = 44; // width of each character
This is how I’m determining where the strikePosition is relative to how the paper is placed in the typewriter, and then using that to determine the # of pixels that remains.
var positionDifference = function (paperPosition){
return strikePosition - paperPosition;
};
var remainingPixels = function (positionDifference) {
return paperWidth - positionDifference;
};
remainingPixels (20);
Everything above is working perfectly. So here’s where things start getting strange. Based on the remaining pixels, I count the typed characters and multiply then by their width:
var charCount = function (){
return charLine.length * charWidth;
};
charCount();
Then… I check whether a “RETURN” is necessary. Given the current parameters, this should return false, but it’s returning true.
var charCheck = function () {
if (charCount <= remainingPixels){
return false;
}
else {
return true;
}
};
charCheck();
Am I missing something?
charCount is a function. So you should be executing it on the if.