Console.logging 然后在 javascript 函数中返回 console.log
Console.logging and then returning the console.log within a javascript function
抱歉,我确定以前有人问过这个问题,但我似乎无法正确地表达它来找到答案。我试图从 Udemy 学习 Javascript 并且有一个问题,你必须 return 这个由星号组成的三角形,其中第一行是 1 个星号,第二行是 2,第三行是 3 等等直到10 行星号。
see this link if I'm not clear enough
我可以 console.log 三角形,但一旦控制台记录了它,我似乎就无法 return 它了。请有人解释我需要在哪里放置 return。我已经尝试了所有我能想到的方法,并且在为 "buildTriangle function".
添加 return 后不断得到未定义或没有答案
/*
* Programming Quiz: Build A Triangle (5-3)
*/
// creates a line of * for a given length
function makeLine(length) {
var line = "";
for (var j = 1; j <= length; j++) {
line += "* ";
}
return line + "\n";
}
// your code goes here. Make sure you call makeLine() in your own code.
function buildTriangle(length){
var tri='';
for(i=0;i<=length;i++){
tri=console.log(makeLine(i));
}
return tri;
}
// test your code by uncommenting the following line
buildTriangle(10);
您应该首先构建三角形然后记录它,即连接三角形变量中的所有行和 return 那:
// creates a line of * for a given length
function makeLine(length) {
var line = "";
for (var j = 1; j <= length; j++) {
line += "* ";
}
return line + "\n";
}
// your code goes here. Make sure you call makeLine() in your own code.
function buildTriangle(length){
var tri='';
for(i=0;i<=length;i++){
tri+=(makeLine(i));
}
return tri;
}
// test your code by uncommenting the following line
console.log(buildTriangle(10));
当您调用 console.log()
方法时,它将 return undefined
(您可以在 console spec 中看到)。相反,您需要在每次循环迭代时将 makeLine(i)
的 return 添加到 tri
字符串(使用 +=
)(将其构建为一个大三角形)。然后,完成后,return 构建的字符串。
除此之外,您应该在循环中的声明前面使用 var/let
并在 i=1
开始循环,因为您不希望结果中有一行零星字符串:
/*
* Programming Quiz: Build A Triangle (5-3)
*/
// creates a line of * for a given length
function makeLine(length) {
let line = "";
for (let j = 1; j <= length; j++) {
line += "* ";
}
return line + "\n";
}
// your code goes here. Make sure you call makeLine() in your own code.
function buildTriangle(length) {
let tri = '';
// \/ -- add let/var here (doesn't effect output in this case) and initialize it to 1
for (let i = 1; i <= length; i++) {
tri += makeLine(i); // call makeLine(i) which returns a string
}
return tri; // return the triangle string
}
// test your code by uncommenting the following line
console.log(buildTriangle(10)); // log the string
抱歉,我确定以前有人问过这个问题,但我似乎无法正确地表达它来找到答案。我试图从 Udemy 学习 Javascript 并且有一个问题,你必须 return 这个由星号组成的三角形,其中第一行是 1 个星号,第二行是 2,第三行是 3 等等直到10 行星号。 see this link if I'm not clear enough
我可以 console.log 三角形,但一旦控制台记录了它,我似乎就无法 return 它了。请有人解释我需要在哪里放置 return。我已经尝试了所有我能想到的方法,并且在为 "buildTriangle function".
添加 return 后不断得到未定义或没有答案
/*
* Programming Quiz: Build A Triangle (5-3)
*/
// creates a line of * for a given length
function makeLine(length) {
var line = "";
for (var j = 1; j <= length; j++) {
line += "* ";
}
return line + "\n";
}
// your code goes here. Make sure you call makeLine() in your own code.
function buildTriangle(length){
var tri='';
for(i=0;i<=length;i++){
tri=console.log(makeLine(i));
}
return tri;
}
// test your code by uncommenting the following line
buildTriangle(10);
您应该首先构建三角形然后记录它,即连接三角形变量中的所有行和 return 那:
// creates a line of * for a given length
function makeLine(length) {
var line = "";
for (var j = 1; j <= length; j++) {
line += "* ";
}
return line + "\n";
}
// your code goes here. Make sure you call makeLine() in your own code.
function buildTriangle(length){
var tri='';
for(i=0;i<=length;i++){
tri+=(makeLine(i));
}
return tri;
}
// test your code by uncommenting the following line
console.log(buildTriangle(10));
当您调用 console.log()
方法时,它将 return undefined
(您可以在 console spec 中看到)。相反,您需要在每次循环迭代时将 makeLine(i)
的 return 添加到 tri
字符串(使用 +=
)(将其构建为一个大三角形)。然后,完成后,return 构建的字符串。
除此之外,您应该在循环中的声明前面使用 var/let
并在 i=1
开始循环,因为您不希望结果中有一行零星字符串:
/*
* Programming Quiz: Build A Triangle (5-3)
*/
// creates a line of * for a given length
function makeLine(length) {
let line = "";
for (let j = 1; j <= length; j++) {
line += "* ";
}
return line + "\n";
}
// your code goes here. Make sure you call makeLine() in your own code.
function buildTriangle(length) {
let tri = '';
// \/ -- add let/var here (doesn't effect output in this case) and initialize it to 1
for (let i = 1; i <= length; i++) {
tri += makeLine(i); // call makeLine(i) which returns a string
}
return tri; // return the triangle string
}
// test your code by uncommenting the following line
console.log(buildTriangle(10)); // log the string