如何使用来自不同阵列的多个索引返回拼接数组

时间:2017-07-15 19:00:12

标签: javascript arrays for-loop splice

dft=["t", "h", "i", "s", "I", "s", "S", "p", "i", "n", "a", "l", "T", "a", "p"];
ans=[4, 6, 12, -1];  
for(x=0;x<ans-1;x++){
dft.splice(ans[x],0,"-");}
return dft;

我正在尝试使用ans数组中除-1引号之外的索引返回一个放在dft数组中的“ - ”数组。

结果我得到的是[“t”,“h”,“i”,“s”,“I”,“s”,“S”,“p”,“i”,“n”,“a “,”l“,”T“,”a“,”p“]

this is the codepen i am working on

5 个答案:

答案 0 :(得分:1)

首先,它没有做任何事情,因为你的for循环结束条件应该是针对x数组的 length 检查循环参数ans,即{{1} }。其次,由于x < ans.length -1正在更改数组,因此在插入第一个连字符后,splice索引将不正确,因此您应该按相反顺序执行此操作:

ans

我们从数组的末尾开始,这将是dft = ["t", "h", "i", "s", "I", "s", "S", "p", "i", "n", "a", "l", "T", "a", "p"]; ans = [4, 6, 12, -1]; for (x = ans.length - 2; x >= 0; x--) { dft.splice(ans[x], 0, "-"); } console.log(dft);,除非你想跳过最后一个元素,所以我们从ans.length - 1开始。请记住,这假定应忽略最后一个元素

答案 1 :(得分:0)

您可以使用Your GPU driver information: GPU #1 Make: 8086 Model: Intel(R) HD Graphics Family Device ID: 0a16 Driver version: 10.18.10.3945 GPU #2 Make: 10de Model: NVIDIA GeForce 820M Device ID: 1140 Driver version: 22.21.13.8476 Some users have experienced emulator stability issues with this driver version. As a result, were selecting a compatibility renderer. Please check with your manufacturer to see if there is an updated driver available.

获得所需的结果

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define alpha_length 26

char secret(char character, int key);

int main(int argc, string argv[]) {
    //check that there are only two strings
    if (argc != 2) {
        printf("Usage: ./vignere k\n");
        return 1;
    }
    //check that argv1 is alphabetical
    string code = argv[1];
    for (int t = 0; t < strlen(code); t++) {    
        if (!isalpha(code[t])) {
            printf("Alphabetical only!\n");
            return 1;
        }
    }
    //get string from user to encrypt
    printf("plaintext: ");
    string plaintext = get_string();

    //array created out of user inputted plain text
    char cypher[strlen(plaintext)];

    //j counts the number of alphabetical characters so that it resets based on argv length
    int j = 0; 
    //iterate over characters in array.  If they are alpha then apply the function secret
    for (int i = 0; i < strlen(plaintext); i++) {
        if (isalpha(plaintext[i])) {
            int index = j % strlen(code);
            int code_index = toupper(code[index]) - 'A' ;
            cypher[i] = secret(plaintext[i], code_index);
            j = j + 1;
        } else {
            cypher[i] = plaintext[i];
        }
    }
    printf("ciphertext: %s\n", cypher);

    return 0;
}  

char secret (char character, int key) {
    char shift;

    // if the character is upper case then start with uppercase A and shift based on the appropriate character from argv1
    if (isupper(character)) {
        shift = (int)character -'A';
        shift = shift + key;
        shift = (shift % alpha_length) + 'A';
    } else {
        // else start wit lower case a
        shift = (int)character - 'a'; 
        shift = shift + key;
        shift = (shift % alpha_length) + 'a';
    }
    return (char)shift;
}

答案 2 :(得分:0)

您需要对索引数组进行排序,并按相反的顺序进行排序,否则会改变其他元素的索引。

&#13;
&#13;
var dft = ["t", "h", "i", "s", "I", "s", "S", "p", "i", "n", "a", "l", "T", "a", "p"],
  ans = [4, 6, 12, -1];

// sor the array to place - at the last higher index first
ans.sort(function(a, b) {
  return a - b;
});
// get array length
var x = ans.length;
// iterate upto index reach to 0 or reaching to -1
while (x-- && ans[x] > -1) {
  dft.splice(ans[x], 0, "-");
}

console.log(dft);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

数组没有数字原语,因此您希望for循环条件读取x&lt; ans.length。

您的索引也需要是ans [x] + x,因为每个splice()都会将数组的长度增加1。

仅这些变化就可以实现您的既定目标。不过我还要补充一下:

  • 你应该有目的地声明变量,而不是使用隐式全局变量。

  • 您可以使用forEach()方法替换for循环,这可以使您的意图更加清晰。

  • 您可能希望区分术语和运算符,这也有助于提高可读性。

答案 4 :(得分:0)

为了完整起见,使用Array#reduceRight

的解决方案

&#13;
&#13;
var array = ["t", "h", "i", "s", "I", "s", "S", "p", "i", "n", "a", "l", "T", "a", "p"],
    indices = [4, 6, 12, -1],
    result = indices.reduceRight((r, i) => (~i && r.splice(i, 0, '-'), r), array);

console.log(result.join(''));
&#13;
&#13;
&#13;