In JavaScript, how would I retain the spaces in a sentence if I split it into an array, then join it?

mtanton

I'm doing a coderbyte challenge with these instructions:

Using the JavaScript language, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.

My code does what the instructions say, but it doesn't retain the spaces of the original sentence.

function LetterChanges(str) {
    var chars = str;
    var newStr = [];

    for (var i = 0; i < str.length; i++) {
        if (/[a-y]/ig.test(chars[i])) {
            newStr[i] = String.fromCharCode(chars[i].charCodeAt(0) + 1);

            if (/[aeiou]/ig.test(newStr[i])) {
                newStr[i] = newStr[i].toUpperCase();
            }
       } else if (/[z]/ig.test(chars[i])) {
            newStr[i] = "A";
       }
    }

    return newStr.join(''); 
}

LetterChanges("Argument goes here"); // result should be: BshvnfOUhpftIfsf

Is there a way to keep the spaces from the original string?

mplungjan

Add

else newStr[i]= chars[i];

To the end of the if/else

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How would I split a sentence into variables, then list it

From Dev

Java , how can i split this sentence into array?

From Dev

How would I break up a sentence?

From Dev

using R How do I retain a sentence with a key word

From Dev

How would I skip spaces in a foreach loop?

From Dev

How can I split a string into tokens in Javascript, including the white spaces?

From Dev

How would I split a stream in Apache Storm?

From Dev

How would I split a form into multiple pages?

From Dev

How would I split this expression with regex?

From Dev

How would i implement JavaScript into this

From Dev

How would I access this with JavaScript?

From Dev

How do I join two worksheets in Excel as I would in SQL?

From Dev

How can I split a sentence based on underscore in Java

From Dev

How can I split a sentence based on underscore in Java

From Dev

How would I deserialize an array like this?

From Dev

How would I print a random array element?

From Dev

How would i group an array in php

From Dev

How do I join an array of strings where each string has spaces?

From Dev

how do i split array by values using javascript

From Dev

how do i split array by values using javascript

From Dev

Split array rows with spaces in Javascript

From Dev

How would I write this in Javascript without jQuery?

From Dev

How would I change the volume in this script? (JavaScript)

From Dev

Javascript: How would I simplify this code?

From Dev

How would I convert an array of multi-dimensions into one single array? Javascript

From Dev

How to retain newlines on xargs -I {}

From Dev

How would I write this query as a join instead of a correlated query?

From Dev

How do I split a string, without spaces, into multiple words in java?

From Dev

How do I split this string in python 2.7 keeping spaces?

Related Related

  1. 1

    How would I split a sentence into variables, then list it

  2. 2

    Java , how can i split this sentence into array?

  3. 3

    How would I break up a sentence?

  4. 4

    using R How do I retain a sentence with a key word

  5. 5

    How would I skip spaces in a foreach loop?

  6. 6

    How can I split a string into tokens in Javascript, including the white spaces?

  7. 7

    How would I split a stream in Apache Storm?

  8. 8

    How would I split a form into multiple pages?

  9. 9

    How would I split this expression with regex?

  10. 10

    How would i implement JavaScript into this

  11. 11

    How would I access this with JavaScript?

  12. 12

    How do I join two worksheets in Excel as I would in SQL?

  13. 13

    How can I split a sentence based on underscore in Java

  14. 14

    How can I split a sentence based on underscore in Java

  15. 15

    How would I deserialize an array like this?

  16. 16

    How would I print a random array element?

  17. 17

    How would i group an array in php

  18. 18

    How do I join an array of strings where each string has spaces?

  19. 19

    how do i split array by values using javascript

  20. 20

    how do i split array by values using javascript

  21. 21

    Split array rows with spaces in Javascript

  22. 22

    How would I write this in Javascript without jQuery?

  23. 23

    How would I change the volume in this script? (JavaScript)

  24. 24

    Javascript: How would I simplify this code?

  25. 25

    How would I convert an array of multi-dimensions into one single array? Javascript

  26. 26

    How to retain newlines on xargs -I {}

  27. 27

    How would I write this query as a join instead of a correlated query?

  28. 28

    How do I split a string, without spaces, into multiple words in java?

  29. 29

    How do I split this string in python 2.7 keeping spaces?

HotTag

Archive