From 42d212bb3c4285253bb523caed022cace80fd485 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Wed, 13 May 2015 19:37:19 -0400 Subject: [PATCH] update solutions to match new exports pattern after removing require --- app/arrays.js | 214 ++++++++++++++++++++-------------------- app/async.js | 40 ++++---- app/bestPractices.js | 50 +++++----- app/config.js | 32 ------ app/count.js | 36 ++++--- app/flowControl.js | 76 +++++++------- app/functions.js | 136 +++++++++++++------------ app/logicalOperators.js | 20 ++-- app/modules.js | 27 +++-- app/numbers.js | 62 ++++++------ app/objects.js | 36 ++++--- app/recursion.js | 121 +++++++++++------------ app/regex.js | 46 +++++---- 13 files changed, 419 insertions(+), 477 deletions(-) delete mode 100644 app/config.js diff --git a/app/arrays.js b/app/arrays.js index 32a2084..5e3fdce 100644 --- a/app/arrays.js +++ b/app/arrays.js @@ -1,135 +1,133 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } - -define(function() { - return { - indexOf : function(arr, item) { - /* - if (Array.prototype.indexOf) { return arr.indexOf(item); } - */ - - for (var i = 0, len = arr.length; i < len; i++) { - if (arr[i] === item) { - return i; - } +exports = (typeof window === 'undefined') ? global : window; + +exports.arraysAnswers = { + indexOf : function(arr, item) { + /* + if (Array.prototype.indexOf) { return arr.indexOf(item); } + */ + + for (var i = 0, len = arr.length; i < len; i++) { + if (arr[i] === item) { + return i; } + } - return -1; - }, + return -1; + }, - sum : function(arr) { - var sum = 0; + sum : function(arr) { + var sum = 0; - for (var i = 0, len = arr.length; i < len; i++) { - sum += arr[i]; - } + for (var i = 0, len = arr.length; i < len; i++) { + sum += arr[i]; + } - return sum; - }, + return sum; + }, - remove : function(arr, item) { - var ret = []; + remove : function(arr, item) { + var ret = []; - for (var i = 0, len = arr.length; i < len; i++) { - if (arr[i] !== item) { - ret.push(arr[i]); - } + for (var i = 0, len = arr.length; i < len; i++) { + if (arr[i] !== item) { + ret.push(arr[i]); } + } - return ret; - }, + return ret; + }, - removeWithoutCopy : function(arr, item) { - var i, len; + removeWithoutCopy : function(arr, item) { + var i, len; - for (i = 0, len = arr.length; i < len; i++) { - if (arr[i] === item) { - arr.splice(i, 1); - i = i - 1; - len = len - 1; - } + for (i = 0, len = arr.length; i < len; i++) { + if (arr[i] === item) { + arr.splice(i, 1); + i = i - 1; + len = len - 1; } + } - return arr; - }, - - append : function(arr, item) { - arr.push(item); - return arr; - }, - - truncate : function(arr) { - arr.pop(); - return arr; - }, - - prepend : function(arr, item) { - arr.unshift(item); - return arr; - }, - - curtail : function(arr) { - arr.shift(arr); - return arr; - }, - - concat : function(arr1, arr2) { - return arr1.concat(arr2); - }, - - insert : function(arr, item, index) { - arr.splice(index, 0, item); - return arr; - }, - - count : function(arr, item) { - var count = 0; - - for (var i = 0, len = arr.length; i < len; i++) { - if (arr[i] === item) { - count++; - } - } + return arr; + }, - return count; - }, + append : function(arr, item) { + arr.push(item); + return arr; + }, - duplicates : function(arr) { - var seen = {}; - var dupes = []; + truncate : function(arr) { + arr.pop(); + return arr; + }, - for (var i = 0, len = arr.length; i < len; i++) { - seen[arr[i]] = seen[arr[i]] ? seen[arr[i]] + 1 : 1; - } + prepend : function(arr, item) { + arr.unshift(item); + return arr; + }, - for (var item in seen) { - if (seen.hasOwnProperty(item) && seen[item] > 1) { - dupes.push(item); - } - } + curtail : function(arr) { + arr.shift(arr); + return arr; + }, + + concat : function(arr1, arr2) { + return arr1.concat(arr2); + }, - return dupes; - }, + insert : function(arr, item, index) { + arr.splice(index, 0, item); + return arr; + }, - square : function(arr) { - var ret = []; + count : function(arr, item) { + var count = 0; - for (var i = 0, len = arr.length; i < len; i++) { - ret.push(arr[i] * arr[i]); + for (var i = 0, len = arr.length; i < len; i++) { + if (arr[i] === item) { + count++; } + } - return ret; - }, + return count; + }, - findAllOccurrences : function(arr, target) { - var ret = []; + duplicates : function(arr) { + var seen = {}; + var dupes = []; - for (var i = 0, len = arr.length; i < len; i++) { - if (arr[i] === target) { - ret.push(i); - } + for (var i = 0, len = arr.length; i < len; i++) { + seen[arr[i]] = seen[arr[i]] ? seen[arr[i]] + 1 : 1; + } + + for (var item in seen) { + if (seen.hasOwnProperty(item) && seen[item] > 1) { + dupes.push(item); } + } - return ret; + return dupes; + }, + + square : function(arr) { + var ret = []; + + for (var i = 0, len = arr.length; i < len; i++) { + ret.push(arr[i] * arr[i]); } - }; -}); + + return ret; + }, + + findAllOccurrences : function(arr, target) { + var ret = []; + + for (var i = 0, len = arr.length; i < len; i++) { + if (arr[i] === target) { + ret.push(i); + } + } + + return ret; + } +}; diff --git a/app/async.js b/app/async.js index d60abb2..a3a0bbd 100644 --- a/app/async.js +++ b/app/async.js @@ -1,26 +1,24 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; -define([ 'jquery' ], function($) { - return { - async : function(value) { - var dfd = $.Deferred(); - setTimeout(function() { - dfd.resolve(value); - }, 10); - return dfd.promise(); - }, +exports.asyncAnswers = { + async : function(value) { + var dfd = $.Deferred(); + setTimeout(function() { + dfd.resolve(value); + }, 10); + return dfd.promise(); + }, - manipulateRemoteData : function(url) { - var dfd = $.Deferred(); + manipulateRemoteData : function(url) { + var dfd = $.Deferred(); - $.ajax(url).then(function(resp) { - var people = $.map(resp.people, function(person) { - return person.name; - }); - dfd.resolve(people.sort()); + $.ajax(url).then(function(resp) { + var people = $.map(resp.people, function(person) { + return person.name; }); + dfd.resolve(people.sort()); + }); - return dfd.promise(); - } - }; -}); + return dfd.promise(); + } +}; diff --git a/app/bestPractices.js b/app/bestPractices.js index b289b92..f008a58 100644 --- a/app/bestPractices.js +++ b/app/bestPractices.js @@ -1,39 +1,37 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; /** * This file defines an object with some methods. Some of these methods are * populated incorrectly; your job is to fix them. Other methods are not * populated at all; your job is to fill them out. */ -define(function() { - return { +exports.bestPracticesAnswers = { - globals : function() { - var myObject = { - name : 'Jory' - }; + globals : function() { + var myObject = { + name : 'Jory' + }; - return myObject; - }, + return myObject; + }, - functions : function(flag) { - var getValue; + functions : function(flag) { + var getValue; - if (flag) { - getValue = function() { return "a"; } - } else { - getValue = function() { return "b"; } - } + if (flag) { + getValue = function() { return "a"; } + } else { + getValue = function() { return "b"; } + } - return getValue(); - }, + return getValue(); + }, - parseInt : function(num) { - return parseInt(num, 10); - }, + parseInt : function(num) { + return parseInt(num, 10); + }, - identity : function(val1, val2) { - return val1 === val2; - } - }; -}); + identity : function(val1, val2) { + return val1 === val2; + } +}; diff --git a/app/config.js b/app/config.js deleted file mode 100644 index 26e681a..0000000 --- a/app/config.js +++ /dev/null @@ -1,32 +0,0 @@ -// Set the require.js configuration for your application. -require.config({ - // Initialize the application with the main application file - deps : [ 'tests/runner' ], - - paths : { - // JavaScript folders - lib : '../lib', - plugins : '../lib/plugins', - tests : '../tests', - app : '../app', - - // Libraries - jquery : '../lib/jquery', - underscore : '../lib/underscore', - backbone : '../lib/backbone', - - // Shim Plugin - use : '../lib/plugins/use', - text : '../lib/plugins/text' - }, - - use : { - underscore : { - attach : '_' - }, - backbone : { - deps : [ 'use!underscore', 'jquery' ], - attach : 'Backbone' - } - } -}); diff --git a/app/count.js b/app/count.js index e8c3e9f..1813c27 100644 --- a/app/count.js +++ b/app/count.js @@ -1,24 +1,22 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; -define(function () { - return { - count : function (start, end) { - var timeout; - function doIt () { - console.log(start++); +exports.countAnswers = { + count : function (start, end) { + var timeout; + function doIt () { + console.log(start++); - if (start <= end) { - timeout = setTimeout(doIt, 100); - } + if (start <= end) { + timeout = setTimeout(doIt, 100); } + } - doIt(); + doIt(); - return { - cancel : function () { - timeout && clearTimeout(timeout); - } - }; - } - }; -}); + return { + cancel : function () { + timeout && clearTimeout(timeout); + } + }; + } +}; diff --git a/app/flowControl.js b/app/flowControl.js index 40b194e..67dcda6 100644 --- a/app/flowControl.js +++ b/app/flowControl.js @@ -1,47 +1,45 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; -define(function() { - return { - fizzBuzz : function(num) { +exports.flowControlAnswers = { + fizzBuzz : function(num) { - // INSTRUCTIONS - // write a function that receives a number as its argument; - // if the number is divisible by 3, the function should return 'fizz'; - // if the number is divisible by 5, the function should return 'buzz'; - // if the number is divisible by 3 and 5, the function should return - // 'fizzbuzz'; - // - // otherwise the function should return the number, or false if no number - // was provided or if the value provided was not a number + // INSTRUCTIONS + // write a function that receives a number as its argument; + // if the number is divisible by 3, the function should return 'fizz'; + // if the number is divisible by 5, the function should return 'buzz'; + // if the number is divisible by 3 and 5, the function should return + // 'fizzbuzz'; + // + // otherwise the function should return the number, or false if no number + // was provided or if the value provided was not a number - // make sure the value provided was a number, if not, return false - if (typeof num !== 'number') { - return false; - } + // make sure the value provided was a number, if not, return false + if (typeof num !== 'number') { + return false; + } - // if the number is divisible by 3 AND 5, then when divided by both, - // the remainder for each operation will be zero - // return 'fizzbuzz' - if (num % 3 === 0 && num % 5 === 0) { - return 'fizzbuzz'; - } + // if the number is divisible by 3 AND 5, then when divided by both, + // the remainder for each operation will be zero + // return 'fizzbuzz' + if (num % 3 === 0 && num % 5 === 0) { + return 'fizzbuzz'; + } - // if the number is divisible by 3, when divided by 3, the remainder is zero - // return 'fizz' - if (num % 3 === 0) { - return 'fizz'; - } + // if the number is divisible by 3, when divided by 3, the remainder is zero + // return 'fizz' + if (num % 3 === 0) { + return 'fizz'; + } - // if the number is divisible by 5, when divided by 5, the remainder is zero - // return 'buzz' - if (num % 5 === 0) { - return 'buzz'; - } + // if the number is divisible by 5, when divided by 5, the remainder is zero + // return 'buzz' + if (num % 5 === 0) { + return 'buzz'; + } - // if the number is not divisible by 3 or 5, i.e. has skipped all previous - // conditions, return the number - return num; + // if the number is not divisible by 3 or 5, i.e. has skipped all previous + // conditions, return the number + return num; - } - }; -}); + } +}; diff --git a/app/functions.js b/app/functions.js index eeeeac2..2f0249d 100644 --- a/app/functions.js +++ b/app/functions.js @@ -1,84 +1,82 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; -define(function() { - return { - argsAsArray : function(fn, arr) { - return fn.apply(null, arr); - }, +exports.functionsAnswers = { + argsAsArray : function(fn, arr) { + return fn.apply(null, arr); + }, - speak : function(fn, obj) { - return fn.call(obj); - }, + speak : function(fn, obj) { + return fn.call(obj); + }, - functionFunction : function(str) { - return function(arg) { - return str + ', ' + arg; - }; - }, - - makeClosures : function(arr, fn) { - var ret = []; - - var makeFn = function(val) { - return function() { return fn(val); }; - }; + functionFunction : function(str) { + return function(arg) { + return str + ', ' + arg; + }; + }, - for (var i = 0, len = arr.length; i < len; i++) { - ret.push(makeFn(arr[i])); - } - - return ret; - }, - - partial : function(fn, str1, str2) { - return function(str3) { - return fn.call(null, str1, str2, str3); - }; - }, + makeClosures : function(arr, fn) { + var ret = []; - useArguments : function() { - var sum = 0; + var makeFn = function(val) { + return function() { return fn(val); }; + }; - for (var i = 0, len = arguments.length; i < len; i++) { - sum += arguments[i]; - } + for (var i = 0, len = arr.length; i < len; i++) { + ret.push(makeFn(arr[i])); + } - return sum; - }, + return ret; + }, - callIt : function(fn) { - var args = Array.prototype.slice.call(arguments, 1, arguments.length); - fn.apply(null, args); - }, + partial : function(fn, str1, str2) { + return function(str3) { + return fn.call(null, str1, str2, str3); + }; + }, - partialUsingArguments : function(fn) { - var args = Array.prototype.slice.call(arguments, 1, arguments.length); - return function() { - var moreArgs = args.concat(Array.prototype.slice.call(arguments)); - return fn.apply(null, moreArgs); - }; - }, + useArguments : function() { + var sum = 0; - curryIt : function(fn) { - function applyArguments(fn, arguments) { - return fn.apply(null, arguments); - } + for (var i = 0, len = arguments.length; i < len; i++) { + sum += arguments[i]; + } - function getArgumentAccumulator(accumulatedArguments, expectedArgumentsCount) { - return function (currentArgument) { - accumulatedArguments.push(currentArgument); + return sum; + }, + + callIt : function(fn) { + var args = Array.prototype.slice.call(arguments, 1, arguments.length); + fn.apply(null, args); + }, + + partialUsingArguments : function(fn) { + var args = Array.prototype.slice.call(arguments, 1, arguments.length); + return function() { + var moreArgs = args.concat(Array.prototype.slice.call(arguments)); + return fn.apply(null, moreArgs); + }; + }, + + curryIt : function(fn) { + function applyArguments(fn, arguments) { + return fn.apply(null, arguments); + } - var allArgumentsProvided = accumulatedArguments.length === expectedArgumentsCount; + function getArgumentAccumulator(accumulatedArguments, expectedArgumentsCount) { + return function (currentArgument) { + accumulatedArguments.push(currentArgument); - if (allArgumentsProvided) { - return applyArguments(fn, accumulatedArguments); - } else { - return getArgumentAccumulator(accumulatedArguments, expectedArgumentsCount); - } - }; - } + var allArgumentsProvided = accumulatedArguments.length === expectedArgumentsCount; - return getArgumentAccumulator([], fn.length); + if (allArgumentsProvided) { + return applyArguments(fn, accumulatedArguments); + } else { + return getArgumentAccumulator(accumulatedArguments, expectedArgumentsCount); + } + }; } - }; -}); + + return getArgumentAccumulator([], fn.length); + } +}; diff --git a/app/logicalOperators.js b/app/logicalOperators.js index 109e996..16275c9 100644 --- a/app/logicalOperators.js +++ b/app/logicalOperators.js @@ -1,13 +1,11 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; -define(function() { - return { - or : function(a, b) { - return a || b; - }, +exports.logicalOperatorsAnswers = { + or : function(a, b) { + return a || b; + }, - and : function(a, b) { - return a && b; - } - }; -}); + and : function(a, b) { + return a && b; + } +}; diff --git a/app/modules.js b/app/modules.js index 9794496..6a23e66 100644 --- a/app/modules.js +++ b/app/modules.js @@ -1,17 +1,14 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; -define(function() { - return { - createModule : function(str1, str2) { - return { - sayIt : function() { - return this.greeting + ', ' + this.name; - }, - - name : str2, - greeting : str1 - }; - } - }; -}); +exports.modulesAnswers = { + createModule : function(str1, str2) { + return { + sayIt : function() { + return this.greeting + ', ' + this.name; + }, + name : str2, + greeting : str1 + }; + } +}; diff --git a/app/numbers.js b/app/numbers.js index 3efb4fe..da0693c 100644 --- a/app/numbers.js +++ b/app/numbers.js @@ -1,45 +1,43 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; -define(function() { - return { - valueAtBit: function(num, bit) { - return 1 & (num >> (bit - 1)); - }, +exports.numbersAnswers = { + valueAtBit: function(num, bit) { + return 1 & (num >> (bit - 1)); + }, - base10: function(str) { - return parseInt(str, 2); - }, + base10: function(str) { + return parseInt(str, 2); + }, - convertToBinary: function(num) { - var arr = []; + convertToBinary: function(num) { + var arr = []; - for (var i = 7; i > -1; i--) { - arr.push( num & (1 << i) ? 1 : 0 ); - } - - return arr.join(''); - }, + for (var i = 7; i > -1; i--) { + arr.push( num & (1 << i) ? 1 : 0 ); + } - multiply: function(a, b) { - a = adjust(a); - b = adjust(b); + return arr.join(''); + }, - var result = (a.adjusted * b.adjusted) / (a.multiplier * b.multiplier); + multiply: function(a, b) { + a = adjust(a); + b = adjust(b); - return result; + var result = (a.adjusted * b.adjusted) / (a.multiplier * b.multiplier); - function adjust(num) { - var exponent, multiplier; + return result; - if (num < 1) { - exponent = Math.floor( Math.log(num) * -1 ); - multiplier = Math.pow(10, exponent); + function adjust(num) { + var exponent, multiplier; - return { adjusted: num * multiplier, multiplier: multiplier }; - } + if (num < 1) { + exponent = Math.floor( Math.log(num) * -1 ); + multiplier = Math.pow(10, exponent); - return { adjusted: num, multiplier: 1 }; + return { adjusted: num * multiplier, multiplier: multiplier }; } + + return { adjusted: num, multiplier: 1 }; } - }; -}); + } +}; diff --git a/app/objects.js b/app/objects.js index 86328fd..2ef0e4f 100644 --- a/app/objects.js +++ b/app/objects.js @@ -1,25 +1,23 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; -define(function() { - return { - alterContext : function(fn, obj) { - return fn.call(obj); - }, +exports.objectsAnswers = { + alterContext : function(fn, obj) { + return fn.call(obj); + }, - alterObjects : function(constructor, greeting) { - constructor.prototype.greeting = greeting; - }, + alterObjects : function(constructor, greeting) { + constructor.prototype.greeting = greeting; + }, - iterate : function(obj) { - var ret = []; + iterate : function(obj) { + var ret = []; - for (var prop in obj) { - if (obj.hasOwnProperty(prop)) { - ret.push(prop + ': ' + obj[prop]); - } + for (var prop in obj) { + if (obj.hasOwnProperty(prop)) { + ret.push(prop + ': ' + obj[prop]); } - - return ret; } - }; -}); + + return ret; + } +}; diff --git a/app/recursion.js b/app/recursion.js index 4a374b6..27d4b32 100644 --- a/app/recursion.js +++ b/app/recursion.js @@ -1,79 +1,76 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } - -define(function() { - return { - listFiles: function(data, dirName) { - var listOfFiles = []; - var dirs = []; - - processDir(data); - - function processDir(dir) { - var i, len, file; - var files = dir.files; - - dirs.push( dir.dir ); - - for (i = 0, len = files.length; i < len; i++) { - file = files[i]; - if (typeof file === 'string') { - if (!dirName || dirs.indexOf(dirName) > -1) { - listOfFiles.push(files[i]); - } - } else { - processDir(files[i]); +exports = (typeof window === 'undefined') ? global : window; + +exports.recursionAnswers = { + listFiles: function(data, dirName) { + var listOfFiles = []; + var dirs = []; + + processDir(data); + + function processDir(dir) { + var i, len, file; + var files = dir.files; + + dirs.push( dir.dir ); + + for (i = 0, len = files.length; i < len; i++) { + file = files[i]; + if (typeof file === 'string') { + if (!dirName || dirs.indexOf(dirName) > -1) { + listOfFiles.push(files[i]); } + } else { + processDir(files[i]); } - - dirs.pop(); } - return listOfFiles; - }, + dirs.pop(); + } - permute: function(arr) { - // http://stackoverflow.com/a/11509565/54468 - var temp = []; - var answer = []; + return listOfFiles; + }, - return doIt(arr); + permute: function(arr) { + // http://stackoverflow.com/a/11509565/54468 + var temp = []; + var answer = []; - function doIt(a) { - var i, len, item; + return doIt(arr); - for (i = 0, len = arr.length; i < len; i++) { - // remove the item at index i - item = arr.splice(i, 1)[0]; + function doIt(a) { + var i, len, item; - // add that item to the array we're building up - temp.push(item); + for (i = 0, len = arr.length; i < len; i++) { + // remove the item at index i + item = arr.splice(i, 1)[0]; - if (arr.length) { - // if there's still anything left in the array, - // recurse over what's left - doIt(arr); - } else { - // otherwise, log the result and move on - logResult(); - } + // add that item to the array we're building up + temp.push(item); - // restore the item we removed at index i - // and remove it from our temp array - arr.splice(i, 0, item); - temp.pop(); + if (arr.length) { + // if there's still anything left in the array, + // recurse over what's left + doIt(arr); + } else { + // otherwise, log the result and move on + logResult(); } - return answer; + // restore the item we removed at index i + // and remove it from our temp array + arr.splice(i, 0, item); + temp.pop(); } - function logResult() { - answer.push( - // make a copy of temp using .slice() - // so we can continue to work with temp - temp.slice() - ); - } + return answer; } - }; -}); + function logResult() { + answer.push( + // make a copy of temp using .slice() + // so we can continue to work with temp + temp.slice() + ); + } + } +}; diff --git a/app/regex.js b/app/regex.js index 35df04b..6f6bd95 100644 --- a/app/regex.js +++ b/app/regex.js @@ -1,30 +1,28 @@ -if (typeof define !== 'function') { var define = require('amdefine')(module); } +exports = (typeof window === 'undefined') ? global : window; -define(function() { - return { - containsNumber : function(str) { - return /\d/.test(str); - }, +exports.regexAnswers = { + containsNumber : function(str) { + return /\d/.test(str); + }, - containsRepeatingLetter : function(str) { - return /([A-Za-z])\1/.test(str); - }, + containsRepeatingLetter : function(str) { + return /([A-Za-z])\1/.test(str); + }, - endsWithVowel : function(str) { - return /[aeiou]$/i.test(str); - }, + endsWithVowel : function(str) { + return /[aeiou]$/i.test(str); + }, - captureThreeNumbers : function(str) { - var matches = /\d{3}/.exec(str); - return matches ? matches[0] : false; - }, + captureThreeNumbers : function(str) { + var matches = /\d{3}/.exec(str); + return matches ? matches[0] : false; + }, - matchesPattern : function(str) { - return /^\d{3}-\d{3}-\d{4}$/.test(str); - }, + matchesPattern : function(str) { + return /^\d{3}-\d{3}-\d{4}$/.test(str); + }, - isUSD : function(str) { - return /^\$\d{1,3}(,\d{3})*(\.\d{2})?$/.test(str); - } - }; -}); + isUSD : function(str) { + return /^\$\d{1,3}(,\d{3})*(\.\d{2})?$/.test(str); + } +};