Javascript typeerrors

I am struggling with a codewars problem:

Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).

It seems that the .shift and .unshift and .toUpperCase arent working because newStr[i] is not a string. I cant figure out why, when i try typeof newStr it says “string”
any help is appreciated

function toCamelCase(str){ let newStr = str.split("-") for (let i = 0; i < newStr.length; i++){ let letter = newStr[i].substring(0, 1) newStr[i].shift letter = letter.toUpperCase() newStr[i].unshift(letter) } return newStr }`Preformatted text`

Hi,
shift is a method you need to invoke as you did with toUpperCase and unshift, but I don’t think that you can apply shift and unshift on strings.

2 Likes

I was under the impression that a string is an array? I remember reading something like that in the full stack course somewhere

A string is a primitive value – like a number, for example. An array is an object:

console.log(typeof [1,2,3]); // "object"
console.log(typeof '123'); // "string"

See more in MDN docs.

Some of the built in array methods can be applied to strings and arrays respectively – like includes(). But they don’t deliver the same result:

const str = 'Hello world!';
const arr = ['Hello', 'world', '!'];
console.log(str.includes('Hel')); // true
console.log(arr.includes('Hel')); // false

Was zur Hölle is wrong with the word ■■■■?
My initial example was blurred out :smile:

1 Like

See:

Specifically:

Strings may be treated as array-like, but they are not array objects, and don’t have access to the array methods just like arrays don’t have access to string methods. As @mirja_t pointed out, there are method names in common, but they aren’t the same methods.

1 Like