还剩3页未读,继续阅读
文本内容:
技术第四章作业Web----------------------------------作业-----------------------------------------
4.2for testing,you mustwrite anXHTML filethat referencesthe JavaScriptfile.Output:The first20Fibonacci numbers,which aredefined asin
4.2the sequence1,2,3,...whereeac.numbe.i.th.sequenc.afte.th.secon.i.th.su.o.th.tw.previou.nu mbers.Yo.mustus.document.writ.t.produc.th.output.!DOCTYPE htmlhtmlheadmetacharset=UTF-8titleFibonacci numbers/titlescriptfunction fx{return x2l:fx-l+fx-2;}function out_f{document.writeThe first20Fibonacci numbers:;forvar i=0;i20;i++{document.writefi+\;}/script/headbodyinput type=button value=Fibonacci numbersonclick=,,out_fM//body/html结果截图☆明C QIocalhost/testl/homework
4.
2.html I©Fibonacci numbers
⑥localhost/testl/home X
①The first20Fibonacci numbers:1,L2,3,5,8,13,21,34,55,89,144,233,377,610r987,1597,2584,4181,6765,作业
4.11Function:counter
4.11Parameter:An arrayof numbers.Returns:The numbersof negativeelements^zeros,and valuesgreater than zero in the givenarray.Note:You mustuse aswitch statementin thefunction.!DOCTYPE htmlhtmlheadmetacharset=UTF-8titlecounter/titlescriptfunction counterlist{//定义记录负数、零、正数个数的数组var i,s=s,并初始化各数据为0switch true{〃进行条件判断负数、零、正数,并对数组中各元素计算个数sfor i=0;ilist.length;i++{case list[i]0:;s
[0]++break;case list[i]===0:;s[l]++break;case list[i]0:;s
[2]++break;return s;}/script/headbodyscript var〃数组my_list_l=[l,5,5,l,5,2,-2,-8,2,0,3,65,5,-6,2,8,5,-15,0];1//数组var my_list_2=[-2,-1,0,0,1,2];2//利用函数计算数组工各var si=countermy_list_l;counter数据个数,并存储在中一一si//利用函数计算数组各var s2=countermy_list_2;counter2数据个数,并存储在中一一sidocument.writeuthe first array:Hmy_list_l/br/;//输出数组工document.write11the second array:Jbr/”;//输出数组2document.writeThe numbersof negativeelements^zeros,and valuesgreater//输出数组的计算结果than zero in the firstarray:br/;1document.writeThe numbersof negativeelements^zeros,and valuesgreater〈;〃输出数组的计算结果thanzero inthe secondarray:,s2J br/”2/script/body/html结果截图:the firstarray:1,5,5,1,5,2-2,-8,2,0,3,65,5,-6,2,8,5,-15,0rthe secondarray:-2-l,00l,2,/,The numbersof negativeelements,zeros,and valuesgreater thanzerointhefirstarray:4,2,13The numbersof negativeelements,zeros,and valuesgreater thanzerointhesecondarray:2,2,2----------------------------------作业-----------------------------------------
4.13Function:row_averages
4.13Parameter:An arrayof arraysof numbers.Returns:An arrayof theaverages ofeach of the rowsofthegiven matrix.!DOCTYPE htmlhtmlheadmetacharset=UTF-8titlerow_averages/titlescript//定义矩阵function row_averagesm{var m〃求矩阵行数列数van x=m.length;x,yvar y=[];//定义每行元素数值之和每行元素数值的平均值var sum=[],n=[];sum,n〃循环求解每行元素平均值fori=0;ix;i++n//求矩阵每行包含元素的个数y[i]=m[i].length;//求每行元素数值之和sum[i]=0;for j=0;jy[i];j++sum{sum[i]=m[i][j]+sum[i];}//求每行元素数值的平均值n[i]=sum[i]/y[i]jn}//返回每行元素数值的平均值return n;n}/script/headbodyscript//定义两个数组var ml=[[11,51,43],[40,15,65],[72,88,98]];var m2=[[1,2,3],[4,5,6],[7,8,9]];var nl=row_averagesml;〃调用函数求两个row_averages数组的行平均值数组nl,n2var n2=row_averagesm2;document.write The first matrix:\br/;//输出第一个矩阵及其平均值forvar k=0;kml.length;k++{document.writeml[k]average:^nlfk]br/;document.writebr/The secondmatrix:\br/;//输出第二个矩阵及其平均值forvar k=0;km
2.length;k++,;document.writem2[k]average:n2[k],”br/”}/script/body/html结果截图:☆D Iocalhost/testl/homework
4....Thefirstmatrix:11,51,43average:3540,15,65average:4072,88,98average:86The secondmatrix:1,2,3average:24,5,6average:57,8,9average:8。
个人认证
优秀文档
获得点赞 0