Trying to solve with match or shift?

Hi,

I had to research online, and I found this which did give me the desired answer but it did not pass, anyhow it is an alternative:

const secretMessage = animals.map(animals =>{
return animals.match(/\b(\w)/g);
})

It didn’t pass because it uses an untaught method, String.match(). Regular Expressions don’t come up this early in the course, either. What I don’t see is how your approach returns only strings of specified length.

2 Likes

Would that be the same reason why this doesn’t pass…or is my approach just wrong in a way I’m not seeing:

const secretMessage = animals.map(string => {
string.shift();
})

My thinking is that since each element is a string, I could use .shift() to rip the first letter out and return it, but that’s not working. Would appreciate any insight/learning you could share

Methods don’t exist globally, there’s no such thing as a .shift that you can use on any value, maybe you mean Array.prototype.shift
Values are defined in specific places, to get ahold of them you’ll need to address that location one way or another.
So if you meant the array method shift, then, that won’t help since you don’t have an array. And, in any case, there’s no need to change the whole array itself when all you’re looking for is the first element.

after some sleep, it makes sense. I think what you’re saying is even though some array methods work on strings, stuff like shift and slice, won’t because it would modify the string. thanks .

No I’m saying array methods work on arrays. If it is behaviour belonging to array, then … it operates on arrays.
shift wouldn’t modify the string, because the string isn’t an array. And also note that you weren’t using Array.prototype.shift, you were trying to get shift from a string, and there’s nothing there, that’s just undefined

If you actually tried though, yes, you would in fact get an error message about strings being immutable.

> Array.prototype.shift.bind('aoeu')()
Thrown:
TypeError: Cannot assign to read only property '0' of object '[object String]'
    at String.shift (<anonymous>)

So then you might think it might work on an object, because that’s mutable

> myobj = {0: 'a', 1: 'b', 2: 'c'}
{ '0': 'a', '1': 'b', '2': 'c' }
> Array.prototype.shift.bind(myobj)()
undefined
> myobj
{ '0': 'a', '1': 'b', '2': 'c', length: 0 }

Well it did something, but the return value is wrong, and the object now got a length attribute for whatever reason, and the first key is still there.

yeah, that’s what had me confused. Then I tried these and they worked

console.log(‘elephant’.length); //prints 8 to screen
console.log(‘elephant’.indexOf(‘p’)); //prints 3 to screen

let word = ‘elephant’
console.log(word.length); //prints 8 to screen
console.log(word.indexOf(‘p’)); //prints 3 to screen

so I assumed shift would work on a string also; got undefined as you suggest. I’ll wait until I’m not a complete noob to noodle it over more;

When you type .shift on a non-array, then that doesn’t refer to array’s shift method.

You’re requesting a property from an object.

const a = { shift: 3 }
const b = { shift: 5 }
const c = {}
console.log(a.shift) // 3
console.log(b.shift) // 5
console.log(c.shift) // undefined. no such property.
console.log([].shift) // [Function]

they may have identically named properties, but they are different things, defined in different locations.

> String.prototype.indexOf === ''.indexOf
true
> String.prototype.indexOf === [].indexOf
false
> Array.prototype.indexOf === ''.indexOf
false
> Array.prototype.indexOf === [].indexOf
true

You probably are requesting a property from an “Object”!!