1

I am trying to randomize each characters casing in a string. My code so far goes as following:

var message = 'This message is for testing'
    
            for (var i = 0; i < message.length; i++) {
                var random_number = Math.round(Math.random() * 10 + 1);
                if(random_number % 2 == 0) {
                    message.charAt(i).toUpperCase();
                }
        }
       console.log(message)

Problem is that even when giving out the letters and numbers via console.log it shows that the numbers would result in an uppercase, but the letters stay low

David Moll
  • 117
  • 10
  • `message.charAt(i).toUpperCase()` returns the character, but you need to do something with it. Strings are immutable anyway, sooooo... – Niet the Dark Absol Aug 07 '20 at 15:55
  • 1
    `console.log(message.replace(/\w/, c=>Math.random()<0.5 ? c : c.toUpperCase()));` – Niet the Dark Absol Aug 07 '20 at 15:55
  • 1
    Does this answer your question? [How do I shuffle the characters in a string in JavaScript?](https://stackoverflow.com/questions/3943772/how-do-i-shuffle-the-characters-in-a-string-in-javascript) – Kunal Mukherjee Aug 07 '20 at 15:57
  • Strings are primatives in javascript which means they cannot be changed. When you run any method on a string like charAt() or toUpperCase(), it returns a new string, and leaves the old one alone. If you create a variable like `var myThing = "foo";` then change it `myThing = "bar"`, you haven't changed the string "foo". It's still "foo". You've only changed what the variable `myThing` is assigned to. Other primatives are booleans, numbers, undefined, null, and NaN. – Charlie Bamford Aug 07 '20 at 16:06

3 Answers3

2

You just need to store your changes, in for example a different string.

var message = 'This message is for testing';
var newMessage = '';

for (var i = 0; i < message.length; i++) {
    var random_number = Math.round(Math.random() * 10 + 1);
    if(random_number % 2 == 0) {
        newMessage += message.charAt(i).toUpperCase();
    }else{
        newMessage += message.charAt(i);
    }
}
console.log(newMessage);
klediooo
  • 630
  • 1
  • 7
  • 25
1

Ciao, concat char to a string like this example:

var message = 'This message is for testing'
let stringresult = "";    
            for (var i = 0; i < message.length; i++) {
                var random_number = Math.round(Math.random() * 10 + 1);
                if(random_number % 2 == 0) {
                    stringresult += message.charAt(i).toUpperCase();
                }
                else stringresult += message.charAt(i);
        }
       console.log(stringresult)
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
  • Faster. :-) Where can I find how to do a snippet, if you don't mind that I'm asking? – klediooo Aug 07 '20 at 16:00
  • 1
    :) yeah but just for couple of seconds... for snippet read [this](https://meta.stackoverflow.com/questions/356678/stack-overflow-run-code-snippet/356679#356679) – Giovanni Esposito Aug 07 '20 at 16:03
1

Build a new string with adding char for char in randomly upper-/lowercase to it.

var message = 'This message is for testing'
let result  ="";
for (var i = 0; i < message.length; i++) {
  var random_number = Math.round(Math.random() * 10 + 1);
  if(random_number % 2 == 0) {
    result += message.charAt(i).toUpperCase();
  } else {
    result += message.charAt(i);
  }
}
console.log(result)
Sascha
  • 4,576
  • 3
  • 13
  • 34