将相等数量的空白添加到字符串的两端的函数 [英] Function that adds equal amount of whitespace to both ends of string

查看:101
本文介绍了将相等数量的空白添加到字符串的两端的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数空间(str,numSpace){
str =;
numSpace =(+);
document.write(numSpace + str + numSpace);
}
console.log(space(hi,));

这并不奏效,我不太确定如何在两端添加空格一个字符串

解决方案


不工作,我不太确定如何将空格添加到
字符串的两端。


你'总是将参数 str 重置为空字符串,因此出现意想不到的行为。

有两种方法可以解决您可以在该方法中使用 console.log(),例如: $ b <1> / b>

 函数空间(str,numSpace){
var result = numSpace + str + numSpace;
console.log(result);
}
space(hi,);

2)您可以返回值,然后使用 console.log()
$ b $ pre $ 函数空间(str,numSpace){
返回numSpace + str + numSpace;
}
var result = space(hi,);
console.log(result);


function space(str,numSpace){
     str = "";
     numSpace = (" " + " ");
     document.write(numSpace + str + numSpace);
}
console.log(space("hi", " "));

This is not working out, I'm not too sure how I can add whitespace to both ends of a string.

解决方案

This is not working out, i'm not too sure how I can add whitespace to both ends of a string.

you're always resetting the parameter str to empty string hence the unexpected behaviour.

there are two ways you could solve the issue at hand.

1) you could use console.log() inside the method, example:

function space(str, numSpace) {
    var result = numSpace + str + numSpace;
    console.log(result);
}
space("hi", " ");

2) you could return the value then use console.log():

function space(str, numSpace) {
    return numSpace + str + numSpace;
}
var result = space("hi", " ");
console.log(result);

这篇关于将相等数量的空白添加到字符串的两端的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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