File tree Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change 4848 }
4949}
5050console . log ( "Solutions with the highest score: " + bestSolutions ) ;
51+
52+
53+ // Actually Frank was right; you can do it one loop.
54+ // But it's more complex. Here's how:
55+ for ( var i = 0 ; i < scores . length ; ++ i ) {
56+ output = "Bubble solution #" + i + " score:" + scores [ i ] ;
57+ console . log ( output ) ;
58+ if ( scores [ i ] > highScore ) {
59+ highScore = scores [ i ] ;
60+ bestSolutions = [ i ] ;
61+ } else if ( scores [ i ] == highScore ) {
62+ bestSolutions . push ( i ) ;
63+ }
64+ }
65+ console . log ( "Best solutions: " + bestSolutions ) ;
66+
67+
5168</ script >
5269</ head >
5370< body >
Original file line number Diff line number Diff line change 6767
6868
6969function getMostCostEffectiveSolution ( scores , costs , highScore ) {
70- var cost = 100.00 ; // much higher than any of the costs
70+ var cost = 100 ; // much higher than any of the costs
7171 var index ;
7272
7373 for ( var i = 0 ; i < scores . length ; i ++ ) {
8787// This is a little more efficient, but not quite as easy to read!
8888//
8989function getMostCostEffectiveSolution2 ( bestSolutions , costs , highScore ) {
90- var cost = 100.0 ;
90+ var cost = 100 ;
9191 var solutionIndex ;
9292 var lowCostIndex ;
9393
You can’t perform that action at this time.
0 commit comments