e4 = prompt("Enter the five favorite sports", "hockey,football,basketball,tennis,golf");
e4 = e4.split(",")
for (var i = 0; i < e4.length; i++) {
if (e4[i] == "football") {
e4[i] = "soccer";
}
}
e5 = prompt("Enter extra sport", "formula 1");
e4.push(e5);
for (var i = 0; i < e4.length; i++) {
e4[i] = e4[i].toUpperCase();
}
e4.sort();
这是我的代码行,可以像这样打印出来
["BASKETBALL", "FORMULA 1", "GOLF", "HOCKEY", "SOCCKER", "TENNIS]
然而,我想要打印出来的格式如下:
BASKETBALL
FORMULA 1
GOLF
HOCKEY
SOCCER
TENNIS
其他代码的新行将从这里开始。
我来自C ++,所以我意识到\n
,但你会如何格式化它,所以它像上面那样缩进,并且每当它通过数组中的元素时创建新行?
答案 0 :(得分:1)
您可以尝试以下内容:
e4 = prompt("Enter the five favorite sports", "hockey,football,basketball,tennis,golf");
e4 = e4.split(",");
for (var i = 0; i < e4.length; i++) {
if (e4[i] == "football") {
e4[i] = "soccer";
}
}
e5 = prompt("Enter extra sport", "formula 1");
e4.push(e5);
e4.sort();
for (var i = 0; i < e4.length; i++) {
e4[i] = e4[i].toUpperCase();
if (i == e4.length) {
e4[i] = " " + e4[i];
} else {
e4[i] = " " + e4[i] + "\n";
}
}
console.log(e4.toString().replace(/,/g, ""));
答案 1 :(得分:1)
IMO,这是一个完美的用例const initialState = {
noteName: []
};
export default function(state = initialState, action) {
switch (action.type) {
case INPUT_VALUE: return {
noteName: [...state.noteName, action.payload]
};
default: return state;
}
}
在Array#join
<br/>
标记作为粘合剂
[].join