我正在尝试在两个偶数之间插入破折号(-)。 问题是破折号不在两个偶数之间,而是在数字的末尾。
这是代码
function insertHyphen(str) {
var strArr = str.split('');
var numArr = strArr.map(Number);
for(var i = 0; i < numArr.length; i++) {
if(numArr[i-1]%2===0 && numArr[i]%2===0) {
numArr.push('-');
}
}
return numArr.join('');
}
insertHyphen('112233445566'); // 112233445566---
答案 0 :(得分:3)
使用splice()
代替push()
。在这里Splice MDN
function insertHyphen(str) {
var strArr = str.split('');
var numArr = strArr.map(Number);
for(var i = 0; i < numArr.length; i++) {
if(numArr[i-1]%2===0 && numArr[i]%2===0) {
numArr.splice(i, 0, '-');
}
}
return numArr.join('');
}
console.log(insertHyphen('112233445566')); // 112-2334-4556-6
答案 1 :(得分:3)
使用正则表达式:
Private Sub MaskedTextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrgNameTextBox.Enter, OrgNameTextBox.Leave
CType(sender, Control).BackColor = Color.Yellow
CType(sender, Control).ForeColor = Color.Black
End Sub
答案 2 :(得分:1)
替换
numArr.push('-');
使用
numArr.splice(i, 0, '-');
答案 3 :(得分:1)
function insertHyphen(str) {
var strArr = str.split('');
var numArr = strArr.map(Number);
for(var i = 0; i < numArr.length; i++) {
if(numArr[i-1]%2===0 && numArr[i]%2===0) {
numArr.splice(i, 0, '-');
}
}
return numArr.join('');
}
console.log(insertHyphen('025468 '));
答案 4 :(得分:0)
Push
在数组的末尾插入元素。您可以使用splice
在特定位置输入元素。
arr.splice(index, 0, item);
此处索引是您要插入item
元素的位置。 0
表示不删除0个元素。
function insertHyphen(str) {
var strArr = str.split('');
var numArr = strArr.map(Number);
for(var i = 0; i < numArr.length; i++) {
if(numArr[i-1]%2===0 && numArr[i]%2===0) {
numArr.splice(i, 0, '-');
}
}
return numArr.join('');
}
console.log(insertHyphen('112233445566'));
答案 5 :(得分:0)
一种简单的方法:
function insertHyphen(str) {
var strArr = str.split('');
var numArr = strArr.map(Number);
var result ="";
for(var i = 0; i < numArr.length; i++) {
if((numArr[i+1]!==undefined)&&(numArr[i]%2===0 && numArr[i+1]%2===0)) {
//numArr.push('-');
result = result + numArr[i] + "-";
}else{
result = result + numArr[i];
}
}
return result;
}
console.log(insertHyphen('112233445566'));
答案 6 :(得分:0)
您可以通过检查值和前任字符串来映射新字符串。
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows for full read/write access to the
// authenticated user's account.
new[] { YouTubeService.Scope.Youtube },
"user",
CancellationToken.None,
new FileDataStore(this.GetType().ToString())
);
}
答案 7 :(得分:0)
使用push()方法和单循环
let input = '346845';
let result = [input[0]],
len = input.length;
for (var i = 1; i < len; i++) {
if (input[i - 1] % 2 === 0 && input[i] % 2 === 0) {
result.push('-', input[i]);
} else {
result.push(input[i]);
}
}
console.log(result.join(''));
答案 8 :(得分:0)
let a = 224578;
let str = a.toString()
var newArray=[arr[0]]
if(arr?.length > 0){
for (let i = 0; i < arr.length; i++) {
if(arr[i] % 2 === 0 && arr[i+1] % 2 === 0){
newArray.push('-', arr[i+1])
} else{
newArray.push(arr[i+1])
}
}
}
console.log('newArray', newArray.join(''))