Caesar cipher error

Bit stuck, why am I getting this error - Cannot assign to read only property ‘1’ of string ‘serr’



function rot13(str) { // LBH QVQ VG!
    	var alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
  	var wordsLC = str.toLowerCase();
  	var words = wordsLC.split(' ');
        var wordStr = "";
  		

  		for(var i = 0;i<words.length;i++){
  			
  			var wordLetters = words[i];
  			console.log("Word: " + wordLetters);


  			for(var j=0;j<wordLetters.length;j++) {
  				var patt = /[^a-zA-Z]/g;
                var pattCheck = patt.test(wordLetters[j]);

                if(pattCheck == true){ 
                	wordStr += wordLetters[j];
                	continue;
                }

	  			var jcount = alphabet.indexOf(wordLetters[j]);
	  			
                jcount += 13;
            

	  			if(jcount >25){
	  				jcount = jcount -26;
	  				wordStr += alphabet[jcount]

	  			}
	  			else{
	  				wordLetters[j] = alphabet[jcount]
	  				wordStr += alphabet[jcount]
	  			}
  		    }

  		    
  			words[i] = wordStr;
  			wordStr = "";
  		
  		}
        var wordsDone = words.join(" ");
       
  		return wordsDone.toUpperCase();
	}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Will do many thanks for your help

Not sure if you figured out the issue with your code. The reason your code produces that error is because behind the scenes, FCC uses "use strict"; which will cause certain errors to show that would normally fail silently. In your case, in the following line:

wordLetters[j] = alphabet[jcount]

you are attempting to mutate a string, which is not possible in JavaScript. You can assign a new value to a string variable, but you can not change individual characters as you are trying to do. If you just remove this line all together, your remaining code allows you to produce a successful solution.

1 Like

Thank you very much Randell, I totally forgot about strings not being mutable (Doh!).
Much appreciated