用JavaScript循环练习 [英] Exercise with a loop in javascript

查看:89
本文介绍了用JavaScript循环练习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一次大学练习才能在屏幕上显示带有文档的文件.使用一种循环将下面的树写成下面的代码:

I need for an university exercise to display onscreen with a document.write the tree below using a kind of loop:

我在for循环的开头使用过,但只打印了第一行...有人可以帮助我吗?

I used at the beginning a for loop but i printed only the first row... someone can help me?

这是我尝试过的:

var numbers = [0, 1, 2, 3, 4] 
for (var i = 0; i <= numbers.length; i++) { 
    if (numbers [i] == 0) { 
        document.write(" * </br>"); 
    } 
    if (numbers [i] == 1) {
        document.write(" *** </br>"); 
    }
    if (numbers [i] == 2) {
        document.write(" ****** </br>"); 
    } 
    if (numbers [i] == 3) {
        document.write(" ******* </br>"); }
    if (numbers [i] == 4) { 
        document.write("********* </br>"); 
    }
    return 
}

谢谢!

推荐答案

我将为您提供该代码的"golfed-ish"(金鱼?应该是东西吗?)版本.换句话说,我可以想到的最小,最模糊的代码可以完成任务.您不应该使用此,因为您的老师无疑会问您这是什么意思,您不会知道,但是我对XD感到无聊

I'm going to give you a "golfed-ish" (goldfish? should this be a thing?) version of the code. In other words, the smallest, most obscure code I can think of to accomplish the task. You should not use this, because your teacher will undoubtedly ask you what it means and you won't know, but I'm bored XD

var size = 5;
document.write("<center>"+Array.apply(0,new Array(size)).map(function(_,i) {return new Array((i+1)*2).join(" * ");}).join("<br>")+"</center>");

演示

正如我所说,请勿使用此:p

As I said, don't use this :p

这篇关于用JavaScript循环练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆