Logical and syntactic error in my currency converter

Asked

Viewed 33 times

-4

Hello, my script has a logical syntactic error. Could anyone help me please.

<html>
<head>
    <title>Conversor</title>
</head>
<body>
    <input type="number" id="num" value=""> 
    <select id="moe1" onchange="converter()" style="display: inline-block;">
        <option value="Euro">€</option>
        <option value="Dolar">$</option>
        <option value="Real">R$</option>
        <option value="Libras">£</option>
    </select>
    <input type="text" id="res" readonly="true">
    <select id="moe2" onchange="converter()" style="display: inline-block;">
        <option value="Euro">€</option>
        <option value="Dolar">$</option>
        <option value="Real">R$</option>
        <option value="Libras">£</option>
    </select>
    <script>
        document.getElementById('num').addEventListener('input', function (evt) {
            converter();
        });
        function converter() {
            var moe1 = document.getElementById('moe1').value
            var moe2 = document.getElementById('moe2').value
            var num = document.getElementById('num').value
            var res = document.getElementById('res')
            if (moe1 == moe2) { 
                res.value = `${num}` 
            }
            if (moe1 == "Euro") {
                if (moe2 == "Dolar") {
                    var calc = num * 1.13
                    res.value = calc.toFixed(2)
                } else if (moe2 == "Real") {
                    var calc = num * 6.02
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Libras") {
                    var calc = num * 0.90
                    res.value = `${calc.toFixed(2)} `
                } 
            if (moe1 == "Dolar") {
                if (moe2 == "Euro") {
                    var calc = num * 0.89
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Real") {
                    var calc = num * 5.33
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Libras") {
                    var calc = num * 0.79
                    res.value = `${calc.toFixed(2)} `
                }
            if (moe1 == "Real") {
                if (moe2 == "Euro") {
                    var calc = num * 0.17
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Dolar") {
                    var calc = num * 0.19
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Libras") {
                    var calc = num * 0.15
                    res.value = `${calc.toFixed(2)} `
                } 
            if (moe1 == "Libras" && moe2 == "Euro") {
                if (moe2 == "Euro") {
                    var calc = num * 1.12
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Dolar") {
                    var calc = num * 1.26
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Real") {
                    var calc = num * 6.73
                    res.value = `${calc.toFixed(2)} `
                }

        }
    </script>
</body>
</html>

  • 2

    Hi João, you are basically asking someone to find out where the error in your code is. This is asking someone to work for you for free. Reduce your code until you find out what causes the error. If a doubt persists after finding out exactly what the bug is, then yes, you ask the question, with a code that exemplifies only doubt. But be sure to ask. Everyone misses. https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-ask-questions

  • When an answer solves your difficulty mark it (the best) as accepted. See https://i.stack.Imgur.com/evLUR.png

2 answers

1

Bro what you trying to do?

1° explain your doubt/difficulty

2° what you have tried?

3° if ask others to solve will never learn.

4° Ta very confused your code if (moe1 == "Euro") { if (moe2 == "Dolar") {

lol

if inside if can’t bro, there’s no way to use a conditional structure within another conditional structure.

and has to print your result.

but be sure to ask for help.

0

Lack of some closing keys }

comparison if (moe1 == "Libras" && moe2 == "Euro") { does not justify

sure is if (moe1 == "Libras") {

limit input value id="num" to 0 and avoid negative numbers

<input type="number" id="num" min=0 oninput="validity.valid||(value='');">

document.getElementById('num').addEventListener('input', function (evt) {
   converter();
});
       
        function converter() {
          var moe1 = document.getElementById('moe1').value
          var moe2 = document.getElementById('moe2').value
          var num = document.getElementById('num').value
          var res = document.getElementById('res')
          

            if (moe1 == moe2) { 
                res.value = `${num}` 
            }
            if (moe1 == "Euro") {
                if (moe2 == "Dolar") {
                    var calc = num * 1.13
                    res.value = calc.toFixed(2)
                } else if (moe2 == "Real") {
                    var calc = num * 6.02
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Libras") {
                    var calc = num * 0.90
                    res.value = `${calc.toFixed(2)} `
                } 
            }
            if (moe1 == "Dolar") {
                if (moe2 == "Euro") {
                    var calc = num * 0.89
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Real") {
                    var calc = num * 5.33
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Libras") {
                    var calc = num * 0.79
                    res.value = `${calc.toFixed(2)} `
                }
            }
            if (moe1 == "Real") {
                if (moe2 == "Euro") {
                    var calc = num * 0.17
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Dolar") {
                    var calc = num * 0.19
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Libras") {
                    var calc = num * 0.15
                    res.value = `${calc.toFixed(2)} `
                } 
            }
            if (moe1 == "Libras") {
                if (moe2 == "Euro") {
                    var calc = num * 1.12
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Dolar") {
                    var calc = num * 1.26
                    res.value = `${calc.toFixed(2)} `
                } else if (moe2 == "Real") {
                    var calc = num * 6.73
                    res.value = `${calc.toFixed(2)} `
                }
            }

        }
<html>
<head>
    <title>Conversor</title>
</head>
<body>
    <input type="number" id="num" value="" min="0"> 
    <select id="moe1" onchange="converter()" style="display: inline-block;">
        <option value="Euro">€</option>
        <option value="Dolar">$</option>
        <option value="Real">R$</option>
        <option value="Libras">£</option>
    </select>
    <input type="text" id="res" readonly="true">
    <select id="moe2" onchange="converter()" style="display: inline-block;">
        <option value="Euro">€</option>
        <option value="Dolar">$</option>
        <option value="Real">R$</option>
        <option value="Libras">£</option>
    </select>
    <script>

    </script>
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.