Home > Software engineering >  My html button isn't working with my js function
My html button isn't working with my js function

Time:08-29

function idk() {
let cypher = {A:"G",B:"L",C:"Z",D:"E",E:"Y",F:"R",G:"D",H:"N",I:"K",J:"W",K:"J",L:"B",M:"Q",N:"F",O:"S",P:"C",Q:"V",R:"X",S:"I",T:"O",U:"M",V:"T",W:"A",X:"U",Y:"H",Z:"P"};
var answer  = prompt('What cypher are you going to use 1 - 26', 1);
var encrypt = prompt('Enter text you want encrypted', 'abc');
var output  = "";

if (answer == 1) {
    encrypt = encrypt.toUpperCase();

    encrypt = encrypt.replace(/[^A-Z]/gm, '');
    encrypt = encrypt.split('');

    encrypt = encrypt.map(letter=> {
        var letterIndex = letter.charCodeAt(0) - 65;
        return Object.values(cypher)[letterIndex];
    })
    document.write(encrypt.join(''));
    
}
function seedCypher() {
    let cypherVals = [], randomLetter;
    while(cypherVals.length < 26){
        do {
            randomLetter = String.fromCharCode(Math.floor(Math.random() * 26)   65);
        } while(cypherVals.indexOf(randomLetter) !== -1);
        cypherVals.push(randomLetter);
    }
    
    let cypherOutput = {};
    cypherVals.forEach((val, i)=>cypherOutput[String.fromCharCode(i  65)] = val);
    return cypherOutput;
}
cypher = seedCypher();
}
/*lol css be empty*/
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>replit</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <button type='button' onClick=idk>Encrypt</button>
  <script src="script.js"></script>
  
</body>

</html>

That's my code. I don't understand why the button isn't working with the function. It has the closed paratheses and the () at the end of the function. I already tried to put the function idk between '' and "", so that doesn't change the output. When I run it, the button will appear, but when I click the button, nothing happens.

CodePudding user response:

You have to add (). Also, you should use "" and it's onclick not onCLick

<button type='button' onclick="idk()">Encrypt</button>

function idk() {
let cypher = {A:"G",B:"L",C:"Z",D:"E",E:"Y",F:"R",G:"D",H:"N",I:"K",J:"W",K:"J",L:"B",M:"Q",N:"F",O:"S",P:"C",Q:"V",R:"X",S:"I",T:"O",U:"M",V:"T",W:"A",X:"U",Y:"H",Z:"P"};
var answer  = prompt('What cypher are you going to use 1 - 26', 1);
var encrypt = prompt('Enter text you want encrypted', 'abc');
var output  = "";

if (answer == 1) {
    encrypt = encrypt.toUpperCase();

    encrypt = encrypt.replace(/[^A-Z]/gm, '');
    encrypt = encrypt.split('');

    encrypt = encrypt.map(letter=> {
        var letterIndex = letter.charCodeAt(0) - 65;
        return Object.values(cypher)[letterIndex];
    })
    document.write(encrypt.join(''));
    
}
function seedCypher() {
    let cypherVals = [], randomLetter;
    while(cypherVals.length < 26){
        do {
            randomLetter = String.fromCharCode(Math.floor(Math.random() * 26)   65);
        } while(cypherVals.indexOf(randomLetter) !== -1);
        cypherVals.push(randomLetter);
    }
    
    let cypherOutput = {};
    cypherVals.forEach((val, i)=>cypherOutput[String.fromCharCode(i  65)] = val);
    return cypherOutput;
}
cypher = seedCypher();
}
/*lol css be empty*/
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>replit</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <button type='button' onclick="idk()">Encrypt</button>
  <script src="script.js"></script>
  
</body>

</html>

  • Related