Title | Java ans |
---|---|
Author | crystal chen |
Course | Information Systems |
Institution | University of Auckland |
Pages | 15 |
File Size | 386.4 KB |
File Type | |
Total Downloads | 45 |
Total Views | 169 |
Java ans...
JavaScript 1 Answers
1. Intro to lessons 2-10 1.1. use alert to show a message alert('HelloMike,welcometoCodeAvengers') 1.2. run code that uses prompt to ask a question Click and answer the questions 1.3. use commands to move a robot robot.forward(5) 1.4. run an example project //Thiscodeaskstheuseraquestion
3.2. do addition and subtraction 7 2 7 3.3. do multiplication and division 32 16 2 3.4. do operations in the correct order 8 10 2 3.5. do operations in the correct order
varanswer=prompt('Whydoyouwanttolearntocode?\n1.To 1 buildcoolapps\n2.Getagreatjob\n3.Makelotsof 2 money\n4.Alloftheabove') 8 1.5. review quiz Review Quiz Questions: 1. 2. 3. 4.
alert * prompt bug
2. Display messages with [[alert]]
4. Basic math calculations 4.1. do a power calculation by multiplying alert(2*2*2*2*2*2*2*2*2*2) 4.2. do a decimal addition alert(16.7+16.7)
2.1. complete the alert message 4.3. use + to join text alert('Mostsoftwarerevenuein2011:MicrosoftUS$54.27 billion,IBMUS$22.49billion') 2.2. use \n to put text on a new line alert('Mostsoftwarerevenuein2011:\nMicrosoftUS$54.27 billion\nIBMUS$22.49billion') 2.3. add data on a new line in an alert message alert('Mostsoftwarerevenuein2011:\nMicrosoftUS$54.27 billion\nIBMUS$22.49billion\nOracleUS$20.96') 2.4. add another alert command alert('Mostsoftwarerevenuein2011:\nMicrosoftUS$54.27 billion\nIBMUS$22.49billion\nOracleUS$20.96') alert('Topwebsites:\nGoogle\nFacebook\nYouTube') 2.5. use "" instead of '' alert("Mostsoftwarerevenuein2011:\nMicrosoftUS$54.27 billion\nIBMUS$22.49billion\nOracleUS$20.96") alert("Topwebsites:\nGoogle\nFacebook\nYouTube")
alert('16.7'+16.7) 4.4. display the result of a calculation with an alert alert((16.7+16.7)/8*1000000000) 4.5. add text to explain the calculation alert('Googledonation$'+(16.7+16.7)/8*1000000000)
5. Store data with '''variables''' 5.1. display calculation results with an alert //showestimatedspreadofworm alert('Numberofinfectedcomputers'+ '\n20min:'+(160000*1.25)+ '\n40min:'+(160000*1.25*1.25)+ '\n60min:'+(160000*1.25*1.25*1.25) ) 5.2. update the calculation
3. Order of operation math rules 3.1. do a simple calculation alert(100‐27)
//showestimatedspreadofworm alert('Numberofinfectedcomputers'+ '\n20min:'+(240000*1.5)+ '\n40min:'+(240000*1.5*1.5)+ '\n60min:'+(240000*1.5*1.5*1.5) )
5.3. create a variable 7.1. move the robot forward varinitial=240000 varrate=1.5 //showestimatedspreadofworm alert('Numberofinfectedcomputers'+ '\n20min:'+(initial*rate)+ '\n40min:'+(initial*rate*rate)+ '\n60min:'+(initial*rate*rate*rate) ) 5.4. update the variables varinitial=280000 varrate=1.4 //showestimatedspreadofworm alert('Numberofinfectedcomputers'+ '\n20min:'+(initial*rate)+ '\n40min:'+(initial*rate*rate)+ '\n60min:'+(initial*rate*rate*rate) )
robot.forward(4) 7.2. turn the robot right robot.right() robot.forward(3) robot.right() robot.forward(2) robot.right() robot.forward(3) 7.3. turn the robot left robot.forward(3) robot.right() robot.forward(1) robot.left() robot.forward(3)
5.5. set variable values using other variables 7.4. turn the robot 180 degrees varinitial=280000 varrate=1.4 var_20min=initial*rate //showestimatedspreadofworm alert('Numberofinfectedcomputers'+ '\n20min:'+(_20min)+ '\n40min:'+(_20min*rate)+ '\n60min:'+(_20min*rate*rate) )
6. Read error messages, fix mistakes 6.1. fix bugs in an alert command alert('VIRUSALERT...Dr.Hackitisatitagain!') 6.2. fix bugs in alert commands
robot.right(2) robot.forward(3) robot.right() robot.forward(2) robot.right() robot.forward(3) robot.left() robot.forward(2) robot.left() robot.forward(3) 7.5. move the robot through a path robot.left() robot.forward(3) robot.left() robot.forward(8) robot.left() robot.forward(5)
alert('VIRUSALERT...Dr.Hackitisatitagain!') alert('AllS.C.R.I.P.T.agentsreporttoyourworkstations immediately!') 6.3. fix bugs in variable names varabbreviatedName='S.C.R.I.P.T.' varfullName='SpecializedCoderRecruitment&Internet ProtectionTaskforce'
8. Ask questions with [[prompt]] 8.1. create a prompt prompt('Whatisyourname?') prompt('Whatisyourage?') 8.2. add default answers to prompts
alert(abbreviatedName+','+fullName) 6.4. fix bugs in variable names
prompt('Whatisyourname?','Anonymous') prompt('Whatisyourage?','21') prompt('Whatisyourgender?','Male')
varabbreviatedName='S.C.R.I.P.T.' varfullName='SpecializedCoderRecruitment&Internet ProtectionTaskforce'
8.3. store user responses with variables
alert(abbreviatedName+','+fullName) varsecretCodeWord='Firewall' alert('The'+abbreviatedName+'secretwordis'+ secretCodeWord) 6.5. fix variable names and () varnoOfAgents=108 varnoOfManagers=32 varnoOfCleaners=20 varweeklyPay=(noOfAgents+noOfManagers)*78000/52+ (noOfCleaners*500) alert('TheaverageweeklypayatS.C.R.I.P.Tis$'+ weeklyPay/(noOfAgents+noOfManagers+noOfCleaners))
7. Move a robot to its goal
varname=prompt('Whatisyourname?','Anonymous') varage=prompt('Whatisyourage?','21') vargender=prompt('Whatisyourgender?','Male') 8.4. use the variable values varname=prompt('Whatisyourname?','Anonymous') varage=prompt('Whatisyourage?','21') vargender=prompt('Whatisyourgender?','Male') alert('Thanksforyourtime'+name) 8.5. give some feedback varname=prompt('Whatisyourname?','Anonymous') varage=prompt('Whatisyourage?','21') vargender=prompt('Whatisyourgender?','Male') varfeedback=prompt('Entersomefeedbackplease:') alert('Thanksforyourtime'+name)
9. Joining strings and numbers with {{+}}
10.5. changing variable values Review Quiz Questions:
9.1. use + to join strings a='CodeAvengers' b='abc' c='abc123' d='46' e='1234'
1. 15 2. 10 3. 5 4. 20
11. Intro to lessons 12-20
9.2. use + with numbers and strings
11.1. review \n
a='Dec2011' b='r2d2' c='2011' d='2000' e='2013'
alert('Keepitup!\n30lessonstogo...') 11.2. review prompt commands varname=prompt('Whatisyourname?','Anonymous') alert('Hello,'+name+',getreadyforlessons12‐20')
9.3. use * and / with strings 11.3. introduction to if statements a='32' b='64' c='50%' d='$48' e='32'
Click run, answer the question by typing spam, then click OK 11.4. detecting cancel Click run then click cancel. Click run again then click OK
9.4. practice using + to join strings a='1KB=1024B' b='75%' c='10GB=10240MB' d='450‐240=210' e='200KB(204800bytes)' 9.5. practice using + to join strings a='5678' b='8daysaweek' c='abceasyas123' d='99bottlesofmilkonthewall' e='24‐7‐365'
10. Review variable name rules 10.1. review variable creation Review Quiz Questions: 1. 2. 3. 4.
var ; var x = 5 var e = z
10.2. review variable name rules Review Quiz Questions: 1. 2. 3. 4.
$ _ full name birth month
10.3. review variable name rules Review Quiz Questions: 1. 2. 3. 4.
1stQuestion #2Q 100dollars fifty%
10.4. review math calculations Review Quiz Questions: 1. 2. 3. 4.
8 12 18 5
11.5. introduction to confirm Click run then click cancel. Click run again then click OK.
12. Check user responses with [[if statements]] 12.1. run an if statement Click run, type Firewall, then click OK 12.2. add another if statement varguess=prompt('WhatistheS.C.R.I.P.T.password?') //DisplayamessageIFtheguessiscorrect if(guess=='Firewall') alert('Correct!') if(guess=='SpamSniper') alert('Intruderalert!') 12.3. change to if–else if–else varguess=prompt('WhatistheS.C.R.I.P.T.password?') //DisplayamessageIFtheguessiscorrect if(guess=='Firewall') alert('Correct!') elseif(guess=='SpamSniper') alert('Intruderalert!') else alert('Incorrect!') 12.4. if statements with multiple statements varguess=prompt('WhatistheS.C.R.I.P.T.password?') //DisplayamessageIFtheguessiscorrect if(guess=='Firewall'){ alert('Correct!') alert('WelcometoS.C.R.I.P.T.') }elseif(guess=='SpamSniper'){ alert('Intruderalert!') }else{ alert('Incorrect!') alert('Pleasetryagain') }
12.5. test all if statement branches
14.3. check the result with an if statement
varguess=prompt('WhatistheS.C.R.I.P.T.password?') //DisplayamessageIFtheguessiscorrect if(guess=='Firewall'){ alert('Correct!') alert('WelcometoS.C.R.I.P.T.') }elseif(guess=='Guest'){ alert('WelcometoS.C.R.I.P.T.') alert('Pleasewaitforyourhost') }elseif(guess=='SpamSniper'){ alert('Intruderalert!') }else{ alert('Incorrect!') alert('Pleasetryagain') }
varclickedOk=confirm('Areyousureyouwanttoleave?') if(clickedOk==true){ alert('Goodbye') } 14.4. testing a confirm box varclickedOk=confirm('Areyousureyouwanttoleave?') if(clickedOk==false){ alert('Thanksforstaying') } 14.5. add another confirm
13. true & false 13.1. greater > or less than < a=true b=false c=true d=false e=true f=true
varclickedOk=confirm('Areyousureyouwanttoleave?') if(clickedOk==true){ clickedOk=confirm('Doyoureallywanttoleave?') } if(clickedOk==false){ alert('Thanksforstaying') }else{ alert('Goodbye') }
13.2. using AND &&, OR || a=true b=false c=false d=true e=false f=true 13.3. using NOT ! a=false b=true c=false d=true e=true f=false 13.4. is equal to == a=false b=true c=true d=true e=true f=false 13.5. using < and > with strings a=false b=true c=false d=true e=false f=true
14. The [[confirm]] command 14.1. confirm box confirm('Areyousureyouwanttoleave?') 14.2. confirm box result varclickedOk=confirm('Areyousureyouwanttoleave?') alert(clickedOk)
15. [[if statement]] practice 15.1. == in if statement conditions Click run, type nothing in the prompt, then click OK. Click run again, type Male, then click OK. Click run again, type Female, then click OK. Click run once more, type boy, then click OK. 15.2. numbers in if statement conditions Click run, type nothing in the prompt, then click OK. Click run again, type 75, then click OK. Click run again, type 135, then click OK. Click run once more, type 75kg, then click OK 15.3. testing if statement conditions Click run, type nothing in the prompt, then click OK. Click run again, type 1.5 then click OK. Click run again, type 2.2, then click OK. Click run again, type 1.85, then click OK. Click run again, type 1.85m, then click OK 15.4. complex if statement conditions Run the code 6 times and do the following: 1. Put nothing in both prompts 2. Type Male for the gender prompt and 7 for the time prompt 3. Type Female then 16 4. Type Male then 15 5. Type Female then type 19 6. Type boy then 20mins 15.5. complex if statement conditions Click run, type 61 and 0, then click OK. Click run again, type 0 and 121, then click OK. Click run again, type 50 and 100, then click OK. Click run once more, type 10 and 10, then click OK
16. Move 2 robots to their goal 16.1. move the robots to the X robot.forward(4) 16.2. move the robots to the X robot.forward(5) robot.right() robot.forward(1)
16.3. move the robots to the X
17.4. if statement practice
robot.forward(4) robot.right() robot.forward(1)
varnativeLanguage=prompt('Whatisyournativelanguage?', 'English') if(nativeLanguage=='Spanish'){ alert('Graciasportutiempo') }elseif(nativeLanguage=='French'){ alert('Mercipourvotretemps') }elseif(nativeLanguage!=null){ alert('Thanksforyourtime') }
if(!robot.onX()){ robot.right(2) robot.forward(1) } 16.4. move the robots to the X robot.forward(4) robot.right() if(robot.atWall()){ robot.right(2) } robot.forward(1) 16.5. move the robots to the X robot.forward(1) if(robot.atWall()){ robot.right() robot.forward(8) robot.right() robot.forward(1) }else{ robot.forward(7) robot.right() robot.forward(2) robot.right() robot.forward(8) }
17. The special value '''null''' 17.1. prompt box cancel button varnativeLanguage=prompt('Whatisyournativelanguage?', 'English') alert(nativeLanguage) 17.2. null value varnativeLanguage=prompt('Whatisyournativelanguage?', 'English') if(nativeLanguage==null){ nativeLanguage=prompt('Whatisyournative language?','English') } 17.3. check for cancel varnativeLanguage=prompt('Whatisyournativelanguage?', 'English') if(nativeLanguage==null){ nativeLanguage=prompt('Whatisyournative language?','English') } if(nativeLanguage!=null){ alert('Thanksforyourtime') }
17.5. using OR || in if statement conditions varnativeLanguage=prompt('Whatisyournativelanguage?', 'English') if(nativeLanguage=='Spanish'||nativeLanguage== 'español'){ alert('Graciasportutiempo') }elseif(nativeLanguage=='French'||nativeLanguage== 'français'){ alert('Mercipourvotretemps') }elseif(nativeLanguage!=null){ alert('Thanksforyourtime') }
18. Read error messages, fix mistakes 18.1. review if statement mistakes varguess=prompt('WhatistheS.C.R.I.P.T.password?') if(guess=='Firewall') alert('Correct!') else alert('Incorrect!') 18.2. review if statement mistakes varguess=prompt('WhatistheS.C.R.I.P.T.password?') if(guess=='Firewall') alert('Correct!') elseif(guess=='Guest') alert('WelcometoS.C.R.I.P.T.') else alert('Incorrect!') 18.3. review if statement mistakes varguess=prompt('WhatistheS.C.R.I.P.T.password?') if(guess=='Firewall') alert('Correct!') elseif(guess=='SpamSniper') alert('Intruderalert!') elseif(guess=='Guest') alert('WelcometoS.C.R.I.P.T.') else alert('Incorrect!') 18.4. review if statement mistakes varguess=prompt('WhatistheS.C.R.I.P.T.password?') if(guess=='Firewall'){ alert('Correct!') alert('WelcometoS.C.R.I.P.T.') }elseif(guess=='SpamSniper'){ alert('Intruderalert!') alert('Robotdispatched') }elseif(guess=='Guest'){ alert('WelcometoS.C.R.I.P.T.') alert('Pleasewaitforyourhost') }else{ alert('Incorrect!') alert('Pleasetryagain') }
18.5. review if statement mistakes
19.5. improve code layout
varguess=prompt('WhatistheS.C.R.I.P.T.password?')
alert('WelcometotheS.C.R.I.P.T.jobrecruitmentsite') varage=prompt('Whatisyourage?') if(age=80){ alert('Sorryyouaretooold') }elseif(age>=22&&age1&&age...