Code challenge about Arrays and strings in pure Javascript

I am a n00b, and am currently doing some pure Javascript challenges to enter a bootcamp.

We are going over arrays, and have learned specifically a few things to complete the hacking challenges, such as:

  • Methods, such as: pop(), push() , split(‘’), join() (or join(’ ') if we want to remove the commas),
  • How to console log what’s inside arrays and concatenate the values with Strings.
  • How to print certain array elements, as so:
var petShop = ['Puppies', 'Kitties', 'Hamsters', 'Snakes', 'Birds'];

console.log('In the second cage we have: ' + petShop[1]);

// prints 'In the second cage we have: Kitties'
  • Array.length
  • How to use for loops to print each of the elements of an array, like so:
var powerRangers = ['Red', 'Black', 'Yellow', 'Pink', 'Blue'];

for( var i = 0; i < powerRangers.length; i++){
    console.log(i + ':' + powerRangers[i]);
}
  • we also went through if statements

CHALLENGE
The challenge states:

Instructions

Copy the uncorrupted data in the array stored in targetDisk to the empty array newDisk (corrupted data looks like this: ø). Then print the contents of the disk to the terminal as a string.

To complete this task remember the arrays’ methods we have learned so far. (which are the ones I wrote above)

MY CODE

var targetDisk = [ 'E', 'ø', '-', 'C', 'ø', 'o', 'r', 'ø', 'ø', 'p', '\'', 'ø', 's', ' ', 'E', 'v', 'ø', 'i', 'ø', 'ø', 'l'];
var newDisk = [];
var corruptionSymbol = 'ø';

var phrase = targetDisk.join(' ');

console.log('This is the phrase: ' + phrase);

var newPhrase = phrase.split('ø');

console.log('This is the newPhrase: ' + newPhrase);

for( var i = 0; i < newPhrase.length; i++){
    newDisk.push(newPhrase[i]);
      //console.log(newDisk);
}

console.log('This is the newDisk: ' + newDisk.join(''));

THE OUTPUT

Code is incorrect.
Your code is not up to fSociety standards, keep trying to improve it
This is the phrase: E ø - C ø o r ø ø p ’ ø s E v ø i ø ø l
This is the newPhrase: E , - C , o r , , p ’ , s E v , i , , l
This is the newDisk: E - C o r p ’ s E v i l


I am thinking that it might be wrong because there are still spaces between the newDisk string - as you can see in the output.

Can someone help me understand what I am doing wrong? How can I remove those spaces between the letters in the final string and have something like so: E-Corp’s Evil

Thank you so much for your help!

For some reason you decided to join targetDisk array using space characters. This is the source of the space characters. There is no reason to do this. You can simply iterate through targetDisk using a for loop and only push elements that are not 'ø' to the newDisk array. You will need some conditional logic to accomplish this.

1 Like

Hi @RandellDawson ,
Thank you so much for your feedback - I had some other tries commented out, and one of them was pretty close to what you described here - just missed a few elements, which made it not work, of course.

Here’s the solution that did work:

for( var i = 0; i < targetDisk.length; i++){
    if(targetDisk[i] !== corruptionSymbol){
        newDisk.push(targetDisk[i]);
	}
};

console.log(newDisk.join(''));

Thank you so much for your help! :slight_smile: