Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Question regarding Arrays and splice

Hello,

I am trying to create a function that promotes an employee among 3 levels, clerk manager and ceo.

I have only just been introduced to the splice method and my code right now, when I call the function once it "fast tracks" the employee all the way to the top when I just want it to promote(or demote) one level at a time, and to promote again would require calling the function again.

As I said, i am not very familiar with the method so can someone explain how I can fix this?

Thanks

var clerks = ['Donner', 'Blitzen', 'Vixen', 'Cupid'];
var managers = ['Comet', 'Dasher', 'Prancer'];
var ceo = ['Santa Claus'];

function promotion(person){
  for(var i = 0; i <= clerks.length; i++){
   if(clerks[i] === person){
       managers.push(clerks[i]);
       console.log(clerks[i] + ' has been promoted to manager!');
       clerks.splice(i,1);
       console.log(clerks, managers, ceo); //to check array contents after function
   } 
  }

  for(var j = 0; j<=managers.length; j++){
    if(managers[j] === person){
      ceo.push(managers[j]);
      console.log(managers[j] + ' has been promoted to CEO!');
      managers.splice(j,1);
      console.log(clerks, managers, ceo);
    }
  }

  for(var k = 0; k<=ceo.length; k++){
    if(ceo[k]===person){
      console.log(ceo[k] + " can't be promoted any higher than CEO!");
    }
  }
  }


function demotion(person){
  for(var j=0; j <= managers.length; j++){
    if(managers[j]===person){
      clerks.push(managers[j]);
      console.log(managers[j] + ' was demoted to clerk :(')
      managers.splice(j,1);
      console.log(clerks, managers, ceo);
      }
  }

  for(var k=0; k <= ceo.length; k++){
    if(ceo[k]===person){
      managers.push(ceo[k]);
      console.log(ceo[k] + ' was demoted to manager :(')
      ceo.splice(k,1);
      console.log(clerks, managers, ceo);
  }
}
    for(var i = 0; i<=clerks.length; i++){
    if(clerks[i]===person){
      console.log(clerks[i] + " can't be demoted any lower than clerk!");
    }
  }
}


console.log(clerks,managers,ceo);
promotion('Blitzen');

2 Answers

The person is being 'fast-tracked' because you are checking the clerks array first and then pushing person to the managers array.
When the function then moves on to check the managers array, the person is now in that array so they get promoted again.

One easy way to solve this issue is to just switch the order that the function checks the arrays. Start at the 'ceo' level and work your way down to clerks.

For example:

function promotion(person){
  for(var k = 0; k<=ceo.length; k++){
    if(ceo[k]===person){
      console.log(ceo[k] + " can't be promoted any higher than CEO!");
    }
  }
    for(var j = 0; j<=managers.length; j++){
    if(managers[j] === person){
      ceo.push(managers[j]);
      console.log(managers[j] + ' has been promoted to CEO!');
      managers.splice(j,1);
      console.log(clerks, managers, ceo);
    }
  }
    for(var i = 0; i <= clerks.length; i++){
   if(clerks[i] === person){
       managers.push(clerks[i]);
       console.log(clerks[i] + ' has been promoted to manager!');
       clerks.splice(i,1);
       console.log(clerks, managers, ceo); //to check array contents after function
   } 
  }
}

Of course >< I should have noticed that. Thanks!

No problem! I'm glad this helped.