File tree Expand file tree Collapse file tree 1 file changed +10
-28
lines changed
Expand file tree Collapse file tree 1 file changed +10
-28
lines changed Original file line number Diff line number Diff line change 2020< body >
2121 < script >
2222 const npcs = [ ] ;
23- const useWhile = true ;
24-
25- if ( useWhile ) {
26- for ( let i = 0 ; i < 10 ; i ++ ) {
27- npcs . push ( createNPC ( ) ) ;
28- }
29- } else {
30- let i = 0 ;
31-
32- while ( i < 10 ) {
33- npcs . push ( createNPC ( ) ) ;
34- i ++ ;
35- }
36- }
37-
38- const npc = npcs [ 0 ] ;
39- console . log ( "npcs[0] has the following properties... " ) ;
40- for ( let prop in npc ) {
41- console . log ( prop + ":" + npc [ prop ] ) ;
42- }
43-
44- let i = 0 ;
45- npcs . forEach ( npc => {
46- console . log ( 'NPC no.' + i + ' uses a ' + npc . weapon + ' as its chosen weapon.' ) ;
47- i ++ ;
48- } )
4923
24+ npcs . push ( createNPC ( ) ) ;
25+ npcs . push ( createNPC ( ) ) ;
26+ npcs . push ( createNPC ( ) ) ;
27+
5028 function createNPC ( ) {
29+ const power = Math . random ( ) * 0.5 + 0.5 ;
30+
5131 const weapons = [ "sword" , "pistol" , "axe" , "spear" ] ;
5232 const weapon = weapons [ Math . floor ( Math . random ( ) * weapons . length ) ] ;
53- const power = Math . random ( ) * 0.5 + 0.5 ;
54- const strength = Math . random ( ) + 1.0 ;
33+
34+ const strength = Math . random ( ) * 2 + 1.0 ;
35+
5536 const damage = 0 ;
37+
5638 return { power, weapon, strength, damage } ;
5739 }
5840 </ script >
You can’t perform that action at this time.
0 commit comments