vendredi 12 janvier 2018

How can I use if else statements to determine a variable's value in javascript

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).

Aucun commentaire:

Enregistrer un commentaire