Kyle Garner
Kyle Garner

Reputation: 25

How to do string replace using if else condition

I am doing a this project on the page about:blank on chrome, meaning there is no other code except for JavaScript involved.

I am trying to make an encryption algorithm with the knowledge that I have. Here is the code so far:

function main() {
    var input = prompt("Enter a string")
    if (input.substring(0, 1) == "a") {
      var _01 = "a"
      var _01new = _01.replace("a", "1")
    }
    else if (input.substring(0, 1) == "b") {
      var _01 = "b"
      var _01new = _01.replace("b", "2")
    }
    else if (input.substring(0, 1) == "c") {
      var _01 = "c"
      var _01new = _01.replace("c", "3")
    }
    else if (input.substring(0, 1) == "d") {
      var _01 = "d"
      var _01new = _01.replace("d", "4")
    }
    else if (input.substring(0, 1) == "e") {
      var _01 = "e"
      var _01new = _01.replace("e", "5")
    }
    else if (input.substring(0, 1) == "f") {
      var _01 = "f"
      var _01new = _01.replace("f", "6")
    }
    else if (input.substring(0, 1) == "g") {
      var _01 = "g"
      var _01new = _01.replace("g", "7")
    }
    else if (input.substring(0, 1) == "h") {
      var _01 = "h"
      var _01new = _01.replace("h", "8")
    }
    else if (input.substring(0, 1) == "i") {
      var _01 = "i"
      var _01new = _01.replace("i", "9")
    }
    else if (input.substring(0, 1) == "j") {
      var _01 = "j"
      var _01new = _01.replace("j", "10")
    }
    else if (input.substring(0, 1) == "k") {
      var _01 = "k"
      var _01new = _01.replace("k", "11")
    }
    else if (input.substring(0, 1) == "l") {
      var _01 = "l"
      var _01new = _01.replace("l", "12")
    }
    else if (input.substring(0, 1) == "m") {
      var _01 = "m"
      var _01new = _01.replace("m", "13")
    }
    else if (input.substring(0, 1) == "n") {
      var _01 = "n"
      var _01new = _01.replace("n", "14")
    }
    else if (input.substring(0, 1) == "o") {
      var _01 = "o"
      var _01new = _01.replace("o", "15")
    }
    else if (input.substring(0, 1) == "p") {
      var _01 = "p"
      var _01new = _01.replace("p", "16")
    }
    else if (input.substring(0, 1) == "q") {
      var _01 = "q"
      var _01new = _01.replace("q", "17")
    }
    else if (input.substring(0, 1) == "r") {
      var _01 = "r"
      var _01new = _01.replace("r", "18")
    }
    else if (input.substring(0, 1) == "s") {
      var _01 = "s"
      var _01new = _01.replace("s", "19")
    }
    else if (input.substring(0, 1) == "t") {
      var _01 = "t"
      var _01new = _01.replace("t", "20")
    }
    else if (input.substring(0, 1) == "u") {
      var _01 = "u"
      var _01new = _01.replace("u", "21")
    }
    else if (input.substring(0, 1) == "v") {
      var _01 = "v"
      var _01new = _01.replace("v", "22")
    }
    else if (input.substring(0, 1) == "w") {
      var _01 = "w"
      var _01new = _01.replace("w", "23")
    }
    else if (input.substring(0, 1) == "x") {
      var _01 = "x"
      var _01new = _01.replace("x", "24")
    }
    else if (input.substring(0, 1) == "y") {
      var _01 = "y"
      var _01new = _01.replace("y", "25")
    }
    else if (input.substring(0, 1) == "z") {
      var _01 = "z"
      var _01new = _01.replace("z", "26")
    }
    else (input.substring(0, 1) == " ")
    {
      var _01 = " "
      var _01new = _01.replace(" ", "27")
    }

    alert(_01new)
}  

main()

I know that there is a lot of code, and I am going to simplify it, but first I am trying to get this to work.

What I am trying to do, is when the user enters a string (1 letter currently) it will find the value of the first letter, detect what it is, and set the value of _01 to whatever the user put in, then use _01.replace to replace the the letter with its assigned value (currently the letters numerical value).

Upvotes: 1

Views: 3407

Answers (1)

Roko C. Buljan
Roko C. Buljan

Reputation: 206131

Your logic will fail for multiple characters.
Your idea of encode(/decode) can only work exclusively if and only if a user enters one character.

What if a user enters say "mm" as string. It's encoded as "1313" and you'll never know how to decode it correctly (well, unless you use array [13,13] or some other ciphering sorcery ;) )
I'll leave this to you.

Now, the answer/suggestion for one character - to - number...

// Create an Object literal with mapped character replacements
var map = {
  "a" : 1,  // Or use some other fancy UTF8 characters or shuffle the order
  "b" : 2,
  "c" : 3,
  // and so on...
  "y" : 25,
  "z" : 26,
  " " : 27
}

function main() {

    var input = prompt("Enter a character!");
    var encrypted = -1; // Fallback number. (If no replacement found in our `map`)

    // check if `map` has this "character" property
    if ( map.hasOwnProperty(input[0]) ) {
        // get the value from our map,
        // where `input[0]` represents the first character
        encrypted = map[input[0]];
    }

    alert( encrypted ); // Will alert the replacement number or log -1 as fallback
}  

main()
TRY USING ONLY ONE OF: "a b c y z (space)"


Here's Something much simpler - but not much secure as-well ;) You could convert a string to base64 using btoa()MDN - and than when you want to read the "secret message" you can simply do btoa()MDN over the encoded string

function main() {

    var input = prompt("Enter a string!").trim();
    var encode = btoa( input ); // String to base64
    var decode = "";
    
    if ( !encode )  return; // Do nothing if nothing valid was entered
    
    alert( encode );
    
    // Whenever you want to reveal your secret message:
    decode = atob( encode );
    alert( "Hey I can also decode! Here you go:\n"+ decode);

}  

main()


I see your interest toward this kind of stuff... so google, learn about cyphers and other encryption algorhitms. Start by exploring the basic ones:

Upvotes: 1

Related Questions