1
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;

I am trying to return an array that has "-" placed into the dft array using the indexes in the ans array except the -1 index.

result im getting is ["t", "h", "i", "s", "I", "s", "S", "p", "i", "n", "a", "l", "T", "a", "p"]

this is the codepen i am working on

0

5 Answers 5

1

Firstly, it's not doing anything because your for loop end condition should be checking the loop parameter x against the length of the ans array, i.e. x < ans.length -1. Secondly, since splice is changing the array, your ans indices will be incorrect after you insert the first hyphen, so you should do it in reverse order like so:

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);

We start at the end of the array, which would be ans.length - 1, except you want to skip the last element, so we start at ans.length - 2. Remember though that this assumes that the last element should be ignored.

Sign up to request clarification or add additional context in comments.

Comments

0

You can reach your desired result with Array#reduce.

let dft = ["t", "h", "i", "s", "I", "s", "S", "p", "i", "n", "a", "l", "T", "a", "p"];
let ans = [4, 6, 12, -1];
let res = dft.reduce((s,a,i) => {
  (ans.indexOf(i) > -1) ? s.push(a, '-') : s.push(a);
  return s;
}, []);

console.log(res);

Comments

0

Arrays do not have a numeric primitive, so you want your for loop condition to read x < ans.length.

Your index also needs to be ans[x] + x, because each splice() is increasing the length of the array by 1.

These changes alone should accomplish your stated goal. However I would also add:

  • you should be declaring variables purposefully, rather than using implicit globals.

  • you can replace your for loop using the forEach() method, which can make your intention more clear.

  • you probably want spacing around terms and operators, which also helps readability.

Comments

0

You need to sort the index array and do in the reverse order otherwise it would change the index of other elements.

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);

Comments

0

Just for completeness, a solution with Array#reduceRight

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(''));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.