I tried to build "Hidden Message" functionality in Java Script. Some sort of decoder..
I want to get the hidden message out of these numbers (it could be any numbers but this is just an example): 25 12 15 24 62 2 42 where a = 1, b = 2.
Right now it works but it’s the other way around.. it works when you enter letter "a" it gives you the value of "1" but I want this to be more simple and I want the number 1 to give me the value "a".
function getSum(total,num) {return total + num};
function val(yazı,harf,değer){ rgxp = new RegExp(değer,'gim'); text = yazı.toLowerCase();
if (text.indexOf(harf) > -1){ sonuc = text.split(harf).join(değer).match(rgxp).map(Number).reduce(getSum) }else{ sonuc=0 };
return sonuc;}
String.prototype.abjad = function() {
a = val(this,'a','1'); b = val(this,'b','2'); c = val(this,'c','3'); ç = val(this,'ç','4'); d = val(this,'d','5');
e = val(this,'e','6'); f = val(this,'f','7'); g = val(this,'g','8'); ğ = val(this,'ğ','9'); h = val(this,'h','10');
ı = val(this,'ı','11'); i = val(this,'i','12'); j = val(this,'j','13'); k = val(this,'k','14'); l = val(this,'l','15');
m = val(this,'m','16'); n = val(this,'n','17'); o = val(this,'o','18'); ö = val(this,'ö','19'); p = val(this,'p','20');
r = val(this,'r','21'); s = val(this,'s','22'); ş = val(this,'ş','23'); t = val(this,'t','24'); u = val(this,'u','25');
ü = val(this,'ü','26'); v = val(this,'v','27'); y = val(this,'y','28'); z = val(this,'z','29');
return a+b+c+ç+d+e+f+g+ğ+h+ı+i+j+k+l+m+n+o+ö+p+r+s+ş+t+u+ü+v+y+z
};
Ask();
function Ask() {
text = prompt("Please enter your numbers");
if (text != null) {
alert("The answer / value is: " + text.abjad())
document.getElementById("result").innerHTML = text.abjad();
}
}
body {
background-color: #093822;
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
color: white;
height:100%;
text-align:center;
font-size:18px;
}
.wrappper{
@include clear;
width:100%;
margin:0 auto;
}
h1, h2, h3 {
font-family: 'Roboto', sans-serif;
font-weight: 100;
text-transform: uppercase;
margin:5px 0;
}
h1 {
font-size: 2.6em;
}
h2 {
font-size: 1.6em;
}
p{
font-size: 1.6em;
}
<div class="wrapper">
<h1>Using this alphabet</h1>
<p>var alphabet = "abcdefghijklmnopqrstuvwxyz !";</p>
<p>Get the hidden message out of these numbers, where a = 1, b = 2</p>
<p>The number are: <b>25 12 15 24 62 2 42</b></p>
</div>
<hr></hr>
<p></p>
<h1>Outcome should appear below:</h1>
The answer / value is: <p id="result"></p>
<p></p>
<hr></hr>
<p>
>Solution :
let alphabet = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUXYZ";
function getNumberFromChar(c){
return alphabet.indexOf(c);
}
function getCharFromNumber(n){
return alphabet[n];
}
function encodeString(text){
return [...text].map(c=>getNumberFromChar(c))
}
function decodeNumbers(numbers){
return numbers.map(n=>getCharFromNumber(n)).join("");
}
const encoded = encodeString("Hello");
console.log(encoded);
console.log(decodeNumbers(encoded));