您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > 南邮Web技术第四章作业
Web技术第四章作业———————————作业4.2—————————————Write,test,anddebug(ifnecessary)JavaScriptscriptsfortheproblemsthatfollow.Whenrequiredtowriteafunction,youmustincludeascripttotestthefunctionwithatleasttwodifferentdatasets.Inallcases,fortesting,youmustwriteanXHTMLfilethatreferencestheJavaScriptfile.4.2Output:Thefirst20Fibonaccinumbers,whicharedefinedasinthesequence1,1,2,3,…whereeachnumberinthesequenceafterthesecondisthesumofthetwopreviousnumbers.Youmustusedocument.writetoproducetheoutput.!DOCTYPEhtmlhtmlheadmetacharset=UTF-8titleFibonaccinumbers/titlescriptfunctionf(x){returnx2?1:(f(x-1)+f(x-2));}functionout_f(){document.write(Thefirst20Fibonaccinumbers:);for(vari=0;i20;i++){document.write(f(i)+,);}}/script/headbodyinputtype=buttonvalue=Fibonaccinumbersonclick=out_f()//body/html结果截图:———————————作业4.11—————————————4.11Function:counterParameter:Anarrayofnumbers.Returns:Thenumbersofnegativeelements,zeros,andvaluesgreaterthanzerointhegivenarray.Note:Youmustuseaswitchstatementinthefunction.!DOCTYPEhtmlhtmlheadmetacharset=UTF-8titlecounter/titlescriptfunctioncounter(list){vari,s=[0,0,0];//定义记录负数、零、正数个数的数组s,并初始化各数据为0for(i=0;ilist.length;i++){switch(true){//进行条件判断负数、零、正数,并对数组s中各元素计算个数case(list[i]0):s[0]++;break;case(list[i]===0):s[1]++;break;case(list[i]0):s[2]++;break;}}returns;}/script/headbodyscriptvarmy_list_1=[1,5,5,1,5,2,-2,-8,2,0,3,65,5,-6,2,8,5,-15,0];//数组1varmy_list_2=[-2,-1,0,0,1,2];//数组2vars1=counter(my_list_1);//利用函数counter计算数组1各数据个数,并存储在s1中vars2=counter(my_list_2);//利用函数counter计算数组2各数据个数,并存储在s1中document.write(thefirstarray:,my_list_1,br/);//输出数组1document.write(thesecondarray:,my_list_2,br/);//输出数组2document.write(Thenumbersofnegativeelements,zeros,andvaluesgreaterthanzerointhefirstarray:,s1,br/);//输出数组1的计算结果document.write(Thenumbersofnegativeelements,zeros,andvaluesgreaterthanzerointhesecondarray:,s2,br/);//输出数组2的计算结果/script/body/html结果截图:———————————作业4.13—————————————4.13Function:row_averagesParameter:Anarrayofarraysofnumbers.Returns:Anarrayoftheaveragesofeachoftherowsofthegivenmatrix.!DOCTYPEhtmlhtmlheadmetacharset=UTF-8titlerow_averages/titlescriptfunctionrow_averages(m){vari,j,m;//定义矩阵mvarx=m.length;//求矩阵行数x,列数yvary=[];varsum=[],n=[];//定义每行元素数值之和sum,每行元素数值的平均值nfor(i=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]);//求每行元素数值的平均值n}returnn;//返回每行元素数值的平均值n}/script/headbodyscriptvarm1=[[11,51,43],//定义两个数组[40,15,65],[72,88,98]];varm2=[[1,2,3],[4,5,6],[7,8,9]];varn1=row_averages(m1);//调用row_averages函数求两个数组的行平均值数组n1,n2varn2=row_averages(m2);document.write(Thefirstmatrix:,br/);//输出第一个矩阵及其平均值for(vark=0;km1.length;k++){document.write(m1[k],average:,n1[k],br/);}document.write(br/,Thesecondmatrix:,br/);//输出第二个矩阵及其平均值for(vark=0;km2.length;k++){document.write(m2[k],average:,n2[k],br/);}/script/body/html结果截图:
本文标题:南邮Web技术第四章作业
链接地址:https://www.777doc.com/doc-4638595 .html