程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了创建带有斜杠和反斜杠的函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决创建带有斜杠和反斜杠的函数?

开发过程中遇到创建带有斜杠和反斜杠的函数的问题如何解决?下面主要结合日常开发的经验,给出你关于创建带有斜杠和反斜杠的函数的解决方法建议,希望对你解决创建带有斜杠和反斜杠的函数有所启发或帮助;

我必须用斜杠和反斜杠在两个上创建一行。我有义务在这个练习中使用几个函数。

这是一个例子:

///////////
\\\\\\\\\\\
///////////
\\\\\\\\\\\
///////////
\\\\\\\\\\\

我的问题是,我在每一行收到一条错误消息“未定义”

///////////
undefined
\\\\\\\\\\\
undefined
///////////
undefined
\\\\\\\\\\\
undefined
///////////
undefined
\\\\\\\\\\\

我不明白问题出在哪里?

displayline();

function slash(){
    var slash = "";
    for(var i=0; i<11; i++){
        slash += "/";
    }
    console.log(slash);
}

function antiSlash(){
    var antiSlash = "";
    for(var i=0; i<11; i++){
        antiSlash += "\\";
    }
    console.log(antiSlash);
}

function displayline(){
    for(var i=0; i<6; i++){
        if(i % 2 === 0){
            console.log(slash());
        } else {
            console.log(antiSlash());
        }
    }
}

解决方法

将部分函数(console.logslash())中的 antiSlash() 更改为 return

displayLine();

function slash(){
    var slash = "";
    for(var i=0; i<11; i++){
        slash += "/";
    }
    return slash;
}

function antiSlash(){
    var antiSlash = "";
    for(var i=0; i<11; i++){
        antiSlash += "\\";
    }
    return antiSlash;
}

function displayLine(){
    for(var i=0; i<6; i++){
        if(i % 2 === 0){
            console.log(slash());
        } else {
            console.log(antiSlash());
        }
    }
}
,

是因slashantiSlash 不返回任何内容。您必须决定,是 slash 返回调用者打印的字符串,还是 slash 进行打印而调用者什么都不做?两者中只有一个需要调用 console.log

,

slashantislash 不返回任何内容,但您正在尝试记录它们返回的内容。删除将调用包装在 console.log 中的 displayLine,这就是输出 undefined 的内容。

或者,保留它们,并将被调用的函数更改为 return 构造的字符串,而不是 loging 它。

,

也许是更好的方法,绝对更短

console.log(
  Array.from(Array(6).keys()).map(v => (v % 2 === 0 ? '/' : '\\').repeat(11)).join('\n')
)

我有义务在这个练习中使用几个函数。

const a = v => v.join('\n')
const c = v => Array.from(Array(v).keys())
const b = v => v.map(v => (v % 2 === 0 ? '/' : '\\').repeat(11))
console.log(
  a(b(c(6)))
)

大佬总结

以上是大佬教程为你收集整理的创建带有斜杠和反斜杠的函数全部内容,希望文章能够帮你解决创建带有斜杠和反斜杠的函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: