tylerl-uxai
tylerl-uxai

Reputation: 80

Removing an extra "" from an array

I'm going to re-write a simple neural network at the command line so I don't run into NPM bullshit from here on out!

The problem is an extra "" gets added to the array. I refuse to write a regex for personal reasons. Let's just say it's unimportant. Let us proceed...

var gulp = require('gulp');

gulp.task('default', function() {

    var trainingSet = "001111101011",
    neuralNetworkStrength = 3;

    var neuralNetwork = []; 

    // e)ssential for loop
    for (var e = 0; e <= (trainingSet.length / neuralNetworkStrength); e++){

        neuralNetwork[e]= trainingSet.substr(e*neuralNetworkStrength,neuralNetworkStrength);

    }


    console.log(neuralNetwork); // [ '001', '111', '101', '011', '' ]



}); // I will eventually remove gulp so I own rights to the code.

Upvotes: 0

Views: 46

Answers (2)

thangngoc89
thangngoc89

Reputation: 1400

Your algorithm is wrong. You must use < operator in the loop instead of <= operator.

Full code:

var trainingSet = "001111101011",
neuralNetworkStrength = 3;

var neuralNetwork = []; 

// essential for loop
for (var e = 0; e < (trainingSet.length / neuralNetworkStrength); e++){

    neuralNetwork[e]= trainingSet.substr(e * neuralNetworkStrength,neuralNetworkStrength);

}


console.log(neuralNetwork);

Upvotes: 4

TechnoCF
TechnoCF

Reputation: 178

Replace the <= in your for loop with <. That should fix your problem.

Upvotes: 2

Related Questions