-1

I've been trying to output the array contents of spliceJoin in Chrome Developer Tools but it always output undefined

Am I missing something?

var wordPermLength = 2;
var wordPerm = ['ab', 'ac', 'a'];
var spliceJoin = [];

function joinWords() {
  for (i = 0; i < wordPermLength; i++) {
    spliceJoin.push(wordPerm[i].concat(wordPerm[i++]));

  }
}
console.log(spliceJoin);
Rajesh
  • 24,354
  • 5
  • 48
  • 79
castlenibill
  • 448
  • 1
  • 6
  • 14
  • You're not running the function `joinWords()`, that's why. When you `console.log(spliceJoin)`, the function hasn't run yet to push them into the array - which rightly prints out `[]` at time of printing. – Albzi Apr 20 '17 at 09:31
  • Also, you can just use `spliceJoin.join('')` – dloeda Apr 20 '17 at 09:32
  • The code you provided actually outputs empty array `[]`. And yes, forgot to run `joinWords` function? – Jevgeni Apr 20 '17 at 09:32
  • btw: why don't you just use `Array.prototype.join()`? Your code becomes like: `var spliceJoin = wordPerm.slice(0, wordPermLength).join("")` – Jevgeni Apr 20 '17 at 09:34
  • Apart from all the above, your function `joinWords` should take the `wordPerm` array as a _parameter_ and it should `return` the permuted array. It should _not_ (as it is currently doing) refer to the variables with those names that are in the outer scope. – Alnitak Apr 20 '17 at 09:34
  • http://stackoverflow.com/questions/351409/how-to-append-something-to-an-array This link will helps you a lot ... @castlenibill – Unknown_Coder Apr 20 '17 at 09:34
  • *in Chrome Developer Tools...* When you assign value to variable, that statement does not returns anything and HENCE `undefined`. Please if thats the issue, try `JSFiddle` or `CodePen` or any other online tool – Rajesh Apr 20 '17 at 09:37
  • 1
    Please explain what you actually want this code to do, because most folks here have ignored the second `i++` that appears inside the loop – Alnitak Apr 20 '17 at 09:41

4 Answers4

0

You missed to call function

joinWords();

You also used i++ which were changing value of i giving wrong result ,Instead of i++ you should use i+1

var wordPermLength = 2;
var wordPerm = ['ab','ac','a'];
var spliceJoin = [];

function joinWords() {
    for (i = 0; i < wordPermLength; i++) {
        spliceJoin.push(wordPerm[i].concat(wordPerm[i+1]));
    }
}
joinWords();
console.log(spliceJoin);
Dhiraj
  • 1,430
  • 11
  • 21
  • If thats the only issue, you should comment it. – Rajesh Apr 20 '17 at 09:35
  • I just realised this is not the problem. When you create a variable and assign a value in **Chrome Dev Tools**, it prints `undefined`. I think OP is asking about this `undefined` – Rajesh Apr 20 '17 at 09:39
  • If you dont find this answer useful you can downvote it – Dhiraj Apr 20 '17 at 09:40
  • I'm not sure about question myself, hence no votes. But my first comment was a suggestion – Rajesh Apr 20 '17 at 09:42
  • @Rajesh , I will consider your suggestion now onwards. Do not take it negative way.thanks for suggestion :) – Dhiraj Apr 20 '17 at 09:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142158/discussion-between-dhiraj-and-rajesh). – Dhiraj Apr 20 '17 at 09:48
-1

It does not return undefined, it's correctly returning [] (empty list). Until you call the joinWords function, the value of spliceJoin will not change, so it will be the empty list you've assigned with var spliceJoin = [];

  • actually his function doesn't return anything at all, so it is in fact implicitly returning `undefined`. – Alnitak Apr 20 '17 at 09:36
-1

Just use the join function in JS.

var wordPermLength = 2;
var wordPerm = ['ab','ac','a'];
var spliceJoin = joinWords();

function joinWords() 
{
    return wordPerm.join('');
}
console.log(spliceJoin);
Biju P Dais
  • 131
  • 1
  • 12
-1

I'd recommend to simplify your code by using following one:

var wordPermLength = 2;
var wordPerm = ['ab', 'ac', 'a'];
var spliceJoin = wordPerm.slice(0, wordPermLength).join("");
console.log(spliceJoin);
Jevgeni
  • 2,556
  • 1
  • 15
  • 18