Secret Message Not Displaying Correctly


#1

Course: https://www.codecademy.com/courses/learn-javascript-arrays/projects/secret-message?course_redirect=introduction-to-javascript

The outcome of the code doesn’t form a message that makes any sense, so, I’m assuming it’s incorrect - but not sure where.

let secretMessage = ["Learning", "isn't", "about", "what", "you", "get", "easily", "the", "first", "time,", "it's", "about", "what", "you", "can", "figure", "out.", "-2015,", "Chris", "Pine,", "Learn", "JavaScript"];

secretMessage.pop();

secretMessage.push('to', 'program');

secretMessage['easily'] = 'right';

secretMessage.shift();

secretMessage.unshift("Programming");

secretMessage.splice('get', 5, "know,");

secretMessage.join(" ");

console.log(secretMessage);

Outcome:

[ 'know,',
  'get',
  'easily',
  'the',
  'first',
  'time,',
  'it\'s',
  'about',
  'what',
  'you',
  'can',
  'figure',
  'out.',
  '-2015,',
  'Chris',
  'Pine,',
  'Learn',
  'to',
  'program',
  easily: 'right' ]


#2

Notice how this line creates a property: value pair? We cannot access values in an array the same way we can with objects, and must use an integer index to access them. Consider the method, indexOf().

The two methods, pop() and shift() can give their values to a variable. This makes it easier for us to document the code…

lastStringInArray = secretMessage.pop();

firstStringInArray = secretMessage.shift();

The splice() method takes two integers as the first two arguments.

This is what the outcome should look like…

Programming isn’t about what you know, it’s about what you can figure out. -2015, Chris Pine, Learn to program


Let’s review the usage of indexOf() and see how it can fit into this problem.

[
  "Learning", "isn't", "about", "what", "you", "get", "easily", "the", 
  "first", "time,", "it's", "about", "what", "you", "can", "figure", "out.",
  "-2015,", "Chris", "Pine,", "Learn", "JavaScript"
];

Your first statement will remove "JavaScript", which will give us,

[
  "Learning", "isn't", "about", "what", "you", "get", "easily", "the", 
  "first", "time,", "it's", "about", "what", "you", "can", "figure", "out.",
  "-2015,", "Chris", "Pine,", "Learn"
];

The second statement will extend the array, giving us,

[
  "Learning", "isn't", "about", "what", "you", "get", "easily", "the", 
  "first", "time,", "it's", "about", "what", "you", "can", "figure", "out.",
  "-2015,", "Chris", "Pine,", "Learn", "to", "program"
];

The third statement is where we need to implement a means of locating the word ‘easily’ in the array.

let position = secretMessage.indexOf('easily');

Now we can swap out that value for the new one, given its correct index.

secretMessage[position] = "right";

So that will give us,

[
  "Learning", "isn't", "about", "what", "you", "get", "right", "the", 
  "first", "time,", "it's", "about", "what", "you", "can", "figure", "out.",
  "-2015,", "Chris", "Pine,", "Learn", "to", "program"
];

The next statement will remove the first element in the array, giving us,

[
  "isn't", "about", "what", "you", "get", "easily", "the", 
  "first", "time,", "it's", "about", "what", "you", "can", "figure", "out.",
  "-2015,", "Chris", "Pine,", "Learn", "to", "program"
];

and now we add a new element to the front of the array, giving us,

[
  "Programming", "isn't", "about", "what", "you", "get", "easily", "the", 
  "first", "time,", "it's", "about", "what", "you", "can", "figure", "out.",
  "-2015,", "Chris", "Pine,", "Learn", "to", "program"
];

Now we get to the interesting part, splice(). Given the syntax,

Array.splice(start, count, value)

We start by locating the first word of the sequence, "get".

let start = secretMessage.indexOf('get');

and now find the index of the last word in the sequence, 'time,'

let end = secretMessage.indexOf('time,');

If we find the difference it will be 1 less than the number words to delete so we need to add one to the difference.

let deleteCount = end - start + 1;

Now we have all the information we need to replace the sequence with the word, 'know,'.

secretMessage.splice(start, deleteCount, "know,");

That gives us this,

[
  "Programming", "isn't", "about", "what", "you",  
  "know,", "it's", "about", "what", "you", "can", "figure", "out.",
  "-2015,", "Chris", "Pine,", "Learn", "to", "program"
];

Rounding all this out, we can build a string from the array using the join() method. The only thing wrong with your statement is that it is not assigned to anything so nothing happens.

secretMessage = secretMessage.join(" ");

console.log(secretMessage);

#3

Amazing! Thanks MTF! :smiley:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.