0

I have a simple problem that's confusing the hell out of me. I tried looking for other similar problems solved but couldn't find this exact problem. I am trying to push a string into an empty string and I'm getting an error is thrown that says the push is not a function?

/Users/3x7r3m157/Development/Javascript/leaderboard.js:37
  competitor.push(data[i])
             ^

TypeError: competitor.push is not a function Super confused. 

Any ideas? Heres my code (I'm aware there are other errors, but currently working through this weird push error):


const args = require('yargs').argv;
const fs = require('fs');
const util = require('util');
const leaderboard = require('./db.json')

const addCompetitor = (name) => {
  leaderboard[name] = { points: [], times: [] }
  console.log(leaderboard)
  return leaderboard
}

console.log(leaderboard)

const addCompetitorTimes = (data) => {
  let parser = 0;
  var competitor = '';
  let times = '';
  let seconds = '';
  let minutes = '';
  let timeInSeconds = 0;

  for (let i = 0 ; i < data.length ; i ++) {
    if (parser == 0) {
      //Stack Overflow peeps, weird error right here:
      competitor.push(data[i])
    }
    if (parser == 0 && i == '_') {
      parser ++
    }
    if (parser == 1) {
      times.push(data[i])
    }
  }

  parser = 0

  for (let i = 0 ; i < times.length ; i ++) {
    if (parser == 0) {
      minutes.push(times[i])
    }
    if (parser == 0 && i == ':') {
      parser ++
    }
    if (parser == 1) {
      seconds.push(times[i])
    }
  }
  seconds = parseInt(seconds);
  minutes = parseInt(minutes);

  timesInSeconds = seconds + (minutes * 60)

  // leaderboard[name].times.push(timeInSeconds)

  return timesInSeconds
}

addCompetitorTimes(args.competitorTimes)

fs.readFile('./db.json', (err, leaderboard) => {
    if (err) throw err;
    var db = JSON.parse(leaderboard);
    // console.log(db)
});


Rashed Hasan
  • 3,721
  • 11
  • 40
  • 82
  • Why do you think a string has a `.push()` method? And what should this `.push()` method do with the strings? – Andreas Oct 12 '19 at 15:44

1 Answers1

1
var competitor = '';

competitor is a string, not an array

Initialise it like this in order to make it an array:

const competitor = [];
Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67