Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Overflow, integers and strings Date: Sat, 02 May 2015 14:21:05 +0100 Organization: A noiseless patient Spider Lines: 35 Message-ID: <87383f5d4u.fsf@bsb.me.uk> References: <09495c64-e320-46ca-a6d9-b9f0427c7efd@googlegroups.com> <66061bf2-9092-42a5-918e-5a039a53db20@googlegroups.com> <6ca07e69-1694-49c1-93eb-748c44d8ab68@googlegroups.com> <871tj08vrb.fsf@bsb.me.uk> <1997740.h7VUlrh2Em@PointedEars.de> <72e44d34-7f4d-4d32-8304-e73688132274@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="20075"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187aMFsJDG/AXMnFcrVv5yM48UyemEAcy4=" Cancel-Lock: sha1:hHqKOQoOtZ/7UswpOUhzQAnQ/+c= sha1:4jG1KyviEiP/uUmZ79bakymSmyw= X-BSB-Auth: 1.b5e24594b36bbc10f599.20150502142105BST.87383f5d4u.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:27470 jonas.thornvall@gmail.com writes: > This works called from main and will do for now. > > function checkArrayOrder() > { > res = arrCompare(arrOne, arrTwo) > if (res) > { > document.calc.stats.value += "Swap! First term smaller=" + res + "\n"; > var tempArr = arrOne.slice(); > arrOne = []; > arrOne = arrTwo.slice(); > arrTwo = []; > arrTwo = tempArr.slice(); > } > } Please don't do that. Write the key function (it's subtract, I think) so that it expect the arrays to be in the right order. Then call it with them in the right order from a wrapper function: function subtractLesserFromGreater(lesser, greater) { ... } function subtract(a, b) { return arrLessThan(a, b) ? subtractLesserFromGreater(a, b) : subtractLesserFromGreater(b, a); } I've changed the name of arrCompare since arrCompare does not give enough information. -- Ben.