@@ -57,7 +57,7 @@ <h3>GPU standard</h3>
5757 </ select >
5858
5959 < h3 > Extras</ h3 >
60- < input type ="checkbox " name ="DVD-Rom " value ="DVD-Rom " id ="DVD " />
60+ < input type ="checkbox " name ="dvdrom " value ="DVD-Rom " id ="DVD " />
6161 < label for ="DVD "> DVD-Rom</ label >
6262 < input type ="checkbox " name ="SoundCard " value ="SoundCard " id ="Sound " />
6363 < label for ="Sound "> Sound Card</ label >
@@ -71,22 +71,119 @@ <h3>Total: $<span id="total">0.00</span></h3>
7171 //exercise 2
7272 var myform = document . form1 ;
7373 var speedprice = 300 , memoryprice = 0 , ssdprice = 0 , hddprice = 0 , gpuprice = 0 , extraprice = 0 , totalprice = 0 ;
74- var cpumultiplier , gpumultiplier ;
74+ var cpumultiplier = 1 , gpumultiplier = 1 ;
7575
7676 myform . speedresult . value = 3.5 ;
77+
78+ function changeCPU ( e ) {
79+ var selectedindex ;
80+ var radios = myform . CPUtype ;
81+ for ( var index = 0 ; index < radios . length ; index ++ ) {
82+ if ( radios [ index ] == e . target ) {
83+ selectedindex = index ;
84+ }
85+ }
86+
87+ if ( selectedindex == 0 ) {
88+ cpumultiplier = 1.2 ;
89+ } else {
90+ cpumultiplier = 1.4 ;
91+ }
92+ addup ( ) ;
93+ }
94+
7795 function newValue ( ) {
7896 var speedValue = myform . CPUspeed . value ;
7997
8098 myform . speedresult . value = speedValue ;
8199 var newspeedValue = ( speedValue - 2.9 ) * 5 ;
82100 speedprice = 100 * newspeedValue ;
101+ speedprice = Math . round ( speedprice ) ;
102+ addup ( ) ;
103+ }
104+
105+ function changeRAM ( ) {
106+ var selectedOption = myform . ram . options [ myform . ram . selectedIndex ] ;
107+ var multiplier = selectedOption . value / 2.0 ;
108+ memoryprice = 10 * multiplier ;
109+ addup ( ) ;
110+ }
111+
112+ function changeSSD ( ) {
113+ var selectedOption = myform . ssd . options [ myform . ssd . selectedIndex ] ;
114+ var multiplier = selectedOption . value / 128.0 ;
115+ ssdprice = 100 * multiplier ;
116+ addup ( ) ;
117+ }
118+
119+ function changeHDD ( ) {
120+ var selectedOption = myform . hdd . options [ myform . hdd . selectedIndex ] ;
121+ var multiplier = selectedOption . value / 512.0 ;
122+ hddprice = 50 * multiplier ;
123+ addup ( ) ;
124+ }
125+
126+ function changeGPU ( ) {
127+ var selectedOption = myform . GPUtype . options [ myform . GPUtype . selectedIndex ] ;
128+ if ( selectedOption . value == "NVIDIA" ) {
129+ gpumultiplier = 1.5 ;
130+ } else {
131+ gpumultiplier = 1.2 ;
132+ }
133+ addup ( ) ;
134+ }
135+
136+ function changeTier ( ) {
137+ var selectedOption = myform . GPUstandard . options [ myform . GPUstandard . selectedIndex ] ;
138+ var tier = selectedOption . value ;
139+ gpuprice = 150 * tier ;
140+ addup ( ) ;
141+ }
142+
143+ function changedvd ( ) {
144+ if ( ! myform . dvdrom . checked ) {
145+ extraprice = extraprice - 60 ;
146+ } else {
147+ extraprice += 60 ;
148+ }
149+ addup ( ) ;
150+ }
151+
152+ function changeSound ( ) {
153+ if ( ! myform . SoundCard . checked ) {
154+ extraprice = extraprice - 50 ;
155+ } else {
156+ extraprice += 50 ;
157+ }
158+ addup ( ) ;
159+ }
160+
161+ function changeNetwork ( ) {
162+ if ( ! myform . Network . checked ) {
163+ extraprice = extraprice - 100 ;
164+ } else {
165+ extraprice += 100 ;
166+ }
83167 addup ( ) ;
84168 }
85169
86170 function addup ( ) {
87- totalprice = speedprice + memoryprice + ssdprice + hddprice + gpuprice + extraprice ;
171+ totalprice = ( speedprice * cpumultiplier ) + memoryprice + ssdprice + hddprice + ( gpuprice * gpumultiplier ) + extraprice ;
172+ document . getElementById ( "total" ) . innerHTML = totalprice ;
173+ }
174+
175+ myform . CPUspeed . addEventListener ( "change" , newValue ) ;
176+ for ( var index = 0 ; index < myform . CPUtype . length ; index ++ ) {
177+ myform . CPUtype [ index ] . addEventListener ( "click" , changeCPU ) ;
88178 }
89- myform . addEventListener ( "change" , newValue ) ;
179+ myform . ram . addEventListener ( "change" , changeRAM ) ;
180+ myform . ssd . addEventListener ( "change" , changeSSD ) ;
181+ myform . hdd . addEventListener ( "change" , changeHDD ) ;
182+ myform . GPUtype . addEventListener ( "change" , changeGPU ) ;
183+ myform . GPUstandard . addEventListener ( "change" , changeTier ) ;
184+ myform . dvdrom . addEventListener ( "click" , changedvd ) ;
185+ myform . SoundCard . addEventListener ( "click" , changeSound ) ;
186+ myform . Network . addEventListener ( "click" , changeNetwork ) ;
90187 </ script >
91188 </ body >
92189</ html >
0 commit comments