2

Can you give me a hint what do I wrong trying to replace space symbol (' ') to ('-').

This is my code:

function hypernateAndLowerCase(strings)
{
 let newArray = [];
 for (let i = 0; i < strings.length; i++) { 
 if (strings[i].indexOf(' ')){
 strings[i].replace(' ', '-');
 }
 newArray.push(strings[i].toLowerCase());
 }
 return newArray;
}
console.log(hypernateAndLowerCase(['HELLO WORLD', 'HELLO YOU']));

vrintle
5,6263 gold badges19 silver badges47 bronze badges
asked Sep 28, 2018 at 10:12

5 Answers 5

3

.replace does not mutate the original string - strings are immutable, so you'd have to explicitly assign the result of using replace for it to work. But, .map is more appropriate here, since you want to transform one array into another:

function hypernateAndLowerCase(strings) {
 return strings.map(string => string.replace(' ', '-').toLowerCase());
}
console.log(hypernateAndLowerCase(['HELLO WORLD', 'HELLO YOU']));

Note that passing a string to .replace will mean that, at most, only one occurrence will be replaced. If you want all occurrences to be replaced, use a global regular expression instead.

answered Sep 28, 2018 at 10:14
Sign up to request clarification or add additional context in comments.

Comments

2

You can simplify your function to following

function hypernateAndLowerCase(strings) {
 return strings.map(v => v.replace(/ /g, "-").toLowerCase());
}
console.log(hypernateAndLowerCase(['HELLO WORLD', 'HELLO YOU']));

answered Sep 28, 2018 at 10:14

Comments

2

You can use .map() and .replace() methods. Use of \s+ in regular expression will allow you to replace one or more spaces with hyphen.

let hypernateAndLowerCase = (data) => data.map(
 str => str.replace(/\s+/g, '-').toLowerCase()
);
console.log(hypernateAndLowerCase(['HELLO WORLD', 'HELLO YOU', 'HELLO WORLD']));

answered Sep 28, 2018 at 10:15

Comments

1

Just doing "strings[i].replace(' ', '-');" won't do anything, you need to reassign the value of each strings[i]. So replace strings[i].replace(' ', '-') with strings[i] = strings[i].replace(' ', '-');

answered Sep 28, 2018 at 10:19

Comments

1

I've created a Fiddle at http://jsfiddle.net/o0bq2rhg/

function hypernateAndLowerCase(strings) {
 let newArray = [];
 for (let i = 0; i < strings.length; i++) {
 if (strings[i].indexOf(' ') > -1) {
 strings[i] = strings[i].replace(/ /g, '-');
 }
 newArray.push(strings[i].toLowerCase());
 }
 return newArray;
}
console.log(hypernateAndLowerCase(['HELLO WORLD', 'HELLO YOU']));

You have to store the outcome of the replaced function back into strings[i]. I've replaced the the function with a regex so you replace all spaces at once: trings[i].replace(/ /g, '-');. Replace only replaces the first occurrence.

answered Sep 28, 2018 at 10:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.