Build this JS calculator in 15 minutes! 🖩

Поділитися
Вставка
  • Опубліковано 4 чер 2024
  • #JavaScript #tutorial #course
    IMPORTANT NOTE: eval() allows you to execute a string of code as if it were actual JavaScript code.
    While it is a useful tool, it is a security risk. I would recommend using eval() ONLY for learning purposes and not any actual production code.
    00:00:00 HTML
    00:03:08 CSS
    00:11:21 JavaScript
    This is a JavaScript project program for beginners where we will build a functioning calculator. Impress your friends!

КОМЕНТАРІ • 281

  • @BroCodez
    @BroCodez  6 місяців тому +145

    IMPORTANT NOTE: eval() allows you to execute a string of code as if it were actual JavaScript code.
    While it is a useful tool, it is a security risk. I would recommend using eval() ONLY for learning purposes and not any actual production code.
    // CALCULATOR PROGRAM
    const display = document.getElementById("display");
    function appendToDisplay(input){
    display.value += input;
    }
    function clearDisplay(){
    display.value = "";
    }
    function calculate(){
    try{
    display.value = eval(display.value);
    }
    catch(error){
    display.value = "Error";
    }
    }


    Document




    +
    7
    8
    9
    -
    4
    5
    6
    *
    1
    2
    3
    /
    0
    .
    =
    C



    body{
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: hsl(0, 0%, 95%);
    }
    #calculator{
    font-family: Arial, sans-serif;
    background-color: hsl(0, 0%, 15%);
    border-radius: 15px;
    max-width: 500px;
    overflow: hidden;
    }
    #display{
    width: 100%;
    padding: 20px;
    font-size: 5rem;
    text-align: left;
    border: none;
    background-color: hsl(0, 0%, 20%);
    color: white;
    }
    #keys{
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    padding: 25px;
    }
    button{
    width: 100px;
    height: 100px;
    border-radius: 50px;
    border: none;
    background-color: hsl(0, 0%, 30%);
    color: white;
    font-size: 3rem;
    font-weight: bold;
    cursor: pointer;
    }
    button:hover{
    background-color: hsl(0, 0%, 40%);
    }
    button:active{
    background-color: hsl(0, 0%, 50%);
    }
    .operator-btn{
    background-color: hsl(35, 100%, 55%);
    }
    .operator-btn:hover{
    background-color: hsl(35, 100%, 65%);
    }
    .operator-btn:active{
    background-color: hsl(35, 100%, 75%);
    }

    • @skappy2000
      @skappy2000 6 місяців тому +11

      Hey bro code big fan here
      I was thinking will you make website projects using html, css and js?
      It will help a lot with people understanding more better in that way
      Portfolio-worthy projects

    • @mirmumit3888
      @mirmumit3888 4 місяці тому

      Can u plz tell me, how to change the only color of the text "Error" on the calculator display?

    • @skappy2000
      @skappy2000 4 місяці тому

      ​@@mirmumit3888
      Use the display.style.color property in script.js file
      function calculate(){
      try{
      display.value = eval(display.value);
      display.style.color = 'white'; // Set text color to white after successful calculation
      }
      catch(error){
      display.value = "Error";
      display.style.color = 'red'; // Set text color to red for error message
      }
      }

    • @SiiamW
      @SiiamW 4 місяці тому +5

      Hey clicking ''." more than once is an error too. this program is slightly wreaked.

    • @oli3641
      @oli3641 3 місяці тому +1

      @@SiiamW well obviously, because 2..4 is not a valid float or integer

  • @alvinderama3196
    @alvinderama3196 6 місяців тому +196

    Watching something being created from scratch is pretty entertaining and educational.

  • @vc6218
    @vc6218 Місяць тому +6

    one of the few videos on here that breaks it down simply. I already understood a bit of html and css - javascript hasn't made that much sense to me until now. Thank you!

  • @watchdoggie8145
    @watchdoggie8145 2 місяці тому +7

    Super thankful for the awesome instructions! keep up the good work.

  • @doronfeldman5152
    @doronfeldman5152 6 місяців тому +57

    You have the best programming channel, I learn html, css, javascript and python in your channel

  • @f4sma
    @f4sma Місяць тому +4

    Your tone of the voice, the way you explain what you are doing.. it's perfect. Subbed!

  • @reptilianis577
    @reptilianis577 6 місяців тому +9

    Another useful video! I am waiting for more ;)

  • @user-ko6hz5lq1d
    @user-ko6hz5lq1d 23 дні тому

    After seeing other videos of writing the code for Calculator app, I find yours the best and easy to understand and needs less lines of code. Thank you very much for uploading this.

  • @Nanashi_29
    @Nanashi_29 2 місяці тому +5

    Best channel about programming

  • @aminahshentour8002
    @aminahshentour8002 14 днів тому

    Never understood JS as much as i did now. I am a loyal fan. Thank you

  • @pope1089
    @pope1089 3 місяці тому +24

    Just started coding, That video helped alot. Straight to the point. I,ll have to check your other videos out before i start asking questions about eval() ans security risks. Thanks

    • @yateen-hi9er
      @yateen-hi9er 2 місяці тому +1

      share some resorses with i am a beginner too

  • @syroxdxd
    @syroxdxd Місяць тому

    I just have to say that you are amazing. I had an introduction class in java and I missed every lecture. I whatched the first 5 hours of your 12hr java course and I passed the exam. You are an amazing teacher. Better than the ones I have at univerity :)

  • @He_Roll2024
    @He_Roll2024 3 місяці тому +2

    Few codes but precised. I'm amazed bro.

  • @muhammet_05
    @muhammet_05 6 місяців тому +7

    Thanks for those teaching video. i love it ❤

  • @levon9
    @levon9 2 дні тому

    Wow .. FANTASTIC - thank you so much for this. Going to look for other videos like this from you. (also subscribed :)

  • @miladevs
    @miladevs 2 дні тому

    Awesome video! I love the clear explanations and the styling tips. Incorporating functional programming principles, like using pure functions, could further enhance the calculator’s logic. It would be interesting to see how this approach can make the code more concise and testable. Keep up the great work! 👍

  • @ZXOT14
    @ZXOT14 Місяць тому

    I don't understand code that well but something about his videos is just so therapeutic.

  • @mohsenvanc
    @mohsenvanc Місяць тому

    that is amazing, thank you

  • @12karmel12
    @12karmel12 6 місяців тому

    thank you for making daily javascript videos

  • @Newbtube92
    @Newbtube92 17 днів тому

    Great tutorial!

  • @sogwafl
    @sogwafl 6 місяців тому +32

    I have a terrible Java Professor, cant retain an oz of her teaching. So I exclusively built my own syllabus off your java topic vids and passed. You are doing gods work and are great at helping my dumbass retain all this info.

    • @ent.8979
      @ent.8979 5 місяців тому +5

      youre not a dumbass, some teachers just don't know how to teach and make the subject impossible to understand. And that's mostly college professor. That's why learning from the internet is becoming more and more valid

    • @russellhan3534
      @russellhan3534 5 місяців тому

      @@ent.8979facts, attending college literally for the degree

    • @lintee_12
      @lintee_12 4 місяці тому +1

      @@ent.8979 i mean its probably because he was getting taught real programming fundamentals... he didn't even know that this was javascript not java

  • @volinh6386
    @volinh6386 4 місяці тому

    your style' css so good , it very butufu

  • @petersimthobele
    @petersimthobele 2 місяці тому +1

    I learnt a lot from this video in 15 min 👏👌

  • @naglakandeel3152
    @naglakandeel3152 Місяць тому

    best teacher ever continue ❤

  • @manueldiegoalexander3879
    @manueldiegoalexander3879 6 місяців тому

    Thanks this is crazy,i like it bro.

  • @jiyoungkim9258
    @jiyoungkim9258 5 місяців тому

    you literally give me confidence

  • @niyonkuruinnocent17
    @niyonkuruinnocent17 2 місяці тому

    You're the best teacher ❤

  • @khushantwankhede8311
    @khushantwankhede8311 3 місяці тому

    thankyou for such a useful content.

  • @elisabethkolar9364
    @elisabethkolar9364 2 місяці тому

    super cool, thank's a lot

  • @TheRealKingofCubing
    @TheRealKingofCubing 2 місяці тому

    This is teaching me so much amazing video

  • @namanthacker
    @namanthacker 2 місяці тому

    Very nice video
    Thanks!!

  • @hurricanos13
    @hurricanos13 6 місяців тому +1

    One of the best coding channel aside with the legendary new boston Bucky Roberts. but i think this channel stuff is also hated by youtube algorithms

  • @Inke7
    @Inke7 6 місяців тому

    Thank you kind sir!

  • @Zaynahazlanworld
    @Zaynahazlanworld 2 дні тому

    thanku .i hv done some typo mistake ,but handle it through chatgpt.thanks for the code

  • @mikesougly
    @mikesougly 2 місяці тому

    Thank you 🙏

  • @mmafights__
    @mmafights__ 3 місяці тому

    soooooo useful thankssssss❤❤

  • @armandopena5508
    @armandopena5508 2 місяці тому +1

    I was hopping to see how you made the actual calculation taking into account that eval is not safe, it would be awesome if you could share how to make a function that at least calculates some basic mathematical operations since this is a very basic project that many people who are starting in programming make, usually without adding the funcitonality, when parsing the math operation is the most important thing of this project!. Great content!

    • @shjsbsbs7186
      @shjsbsbs7186 2 місяці тому

      Agree, eval is not safe for newbie

    • @larslover6559
      @larslover6559 2 місяці тому

      Theres several nice tutorial on python to make calculator without the eval function. There you really see it hard coded from scractch!

  • @hunin27
    @hunin27 6 місяців тому +1

    Thanks!!!

  • @habibousy8723
    @habibousy8723 22 дні тому

    GREAT I'm senegalese and my english is not perfect but I learn a lot because of you😎😎😎. My javascript futur will be bright with you😎😎

  • @nazmussakib9346
    @nazmussakib9346 Місяць тому

    Thank you...❤

  • @coderprakash
    @coderprakash 6 місяців тому

    Hi please make a complete tutorial on typescript 🙏 i followed ur javascript and react thanks alot ❤️🔥 love from India, I know Frontend development because of u please consider my request and make typescript full tutorial

  • @PurplexSKyo
    @PurplexSKyo 6 місяців тому +6

    Hello Bro Code
    I love your videos, I try to learn C by looking your videos and its amazing
    I know its a litter bit late for tell you this but can you please do a tutorial learn Lua please if its don't mind you
    Thanks you for having the time to look the lines and keep continute
    I love your videos👍🏾👍🏾

    • @mcpigeon6908
      @mcpigeon6908 6 місяців тому +2

      yes I'd love to learn Lua too :)

  • @CharukaRanathunga-bi6fy
    @CharukaRanathunga-bi6fy 3 місяці тому

    thank you 👍

  • @hunin27
    @hunin27 6 місяців тому +13

    Wow, then what we are doing is pretty much just styling. I had no idea javascript had a "built-in" calculator inside of a function!!

    • @BroCodez
      @BroCodez  6 місяців тому +8

      eval() runs a JS expression dynamically at runtime. It can be a security risk tho

    • @hunin27
      @hunin27 6 місяців тому

      oh, well im pretty sure its not that hard to make a calculator without using it. i mean eval is a function and we could just recreate it@@BroCodez

    • @lelin4732
      @lelin4732 4 місяці тому

      You can have 100 years of JS experience but still be encountering something damn new 💀 Just a beauty of coding

  • @kursatepik5197
    @kursatepik5197 6 місяців тому

    you are a hero brother

  • @user-un9fs3oj5r
    @user-un9fs3oj5r 2 місяці тому

    thenkyou very much

  • @ciro3857
    @ciro3857 22 дні тому

    Thank u lad

  • @4chillingout
    @4chillingout 2 місяці тому

    So educational video ever seen

  • @cristiandalessandro599
    @cristiandalessandro599 6 місяців тому

    Are all these videos going to be part of an updated full course?

  • @user-id5vh7rq3j
    @user-id5vh7rq3j 5 місяців тому +4

    I think this was the easiest calculator build and explanation from any other youtube channel

    • @samueldayo9261
      @samueldayo9261 4 місяці тому

      Was about to comment this yesterday it is the best

  • @paraglide01
    @paraglide01 5 місяців тому +2

    Wauw, thanks man this is great. Making a calculator creates so many ideas for apps that need input and calculating.

  • @subhro3044
    @subhro3044 4 місяці тому +1

    Yo thanks for the video tutorial as someone who's starting out with coding HTML this was super fun to do as a project
    Just one question though when I tried to replicate it and open the index.html file as *Open with Live Server* I'm just getting the basic buttons that was done at the start
    Am I opening/running it the wrong way?

    • @Kokoclouds
      @Kokoclouds 2 місяці тому +1

      same here for me, i dunno what i'm doing wrong either. checked all the line, everything matches but still not getting the round buttons

  • @berbahc
    @berbahc 5 місяців тому

    so cool thanks bro

  • @neerajmeka25
    @neerajmeka25 3 місяці тому +1

    Great Project had fun to build this project, u got a new sub will check your more videos

  • @leobertobittencourtfilho3036
    @leobertobittencourtfilho3036 Місяць тому

    I wish (and I'm trying to) that I could code at this level. Honestly!

  • @godcomplex1929
    @godcomplex1929 6 місяців тому +5

    Sir, I think we are due a database tutorial.
    I was tryna learn on MariaDB since it was free but couldn't get anywhere without your detailed guide.

  • @Channel3-lf9sg
    @Channel3-lf9sg 20 годин тому

    Thanks

  • @omidrezafeizi3530
    @omidrezafeizi3530 Місяць тому

    very informative

  • @siampro3891
    @siampro3891 5 місяців тому +1

    BEST👍👍👍👍

  • @clementsim7227
    @clementsim7227 3 місяці тому

    Hi! I have a question! Why is it that the appendToDisplay function only works when I remove type="module" within my script tags in the HTML document?

  • @StephenColeman-yj5vo
    @StephenColeman-yj5vo Місяць тому

    Amazing

  • @paulpaul6042
    @paulpaul6042 5 місяців тому

    can we do a bubble we can write on it and with a option to add other or delete it? and see it on a 3 D?

  • @user-xj7te3qs8u
    @user-xj7te3qs8u 6 місяців тому

    Great 😊 😊😊

  • @hesa4196
    @hesa4196 5 місяців тому

    Nice!

  • @n.n.g2230
    @n.n.g2230 3 місяці тому

    yo bro code, thanks as always for a great video and motivation to study!

  • @danielbeeber5587
    @danielbeeber5587 Місяць тому

    Awesome

  • @vitorh.4567
    @vitorh.4567 28 днів тому

    awesome video

  • @technicalresi5451
    @technicalresi5451 6 місяців тому

    Very nice bro 😊❤

  • @not-that-epic
    @not-that-epic 19 годин тому

    yeah but like what software do you use for coding??

  • @moontvdyyds6846
    @moontvdyyds6846 29 днів тому

    thanks lots

  • @jagannathan1014
    @jagannathan1014 3 дні тому

    add this to appendToDisplay function: if(display.value=="Error"){ display.value=input;}

  • @zahersankari7342
    @zahersankari7342 Місяць тому

    Why so simple?!?
    Thanks!

  • @pope1089
    @pope1089 3 місяці тому

    How would you bind the calculator to your pc number pad.

  • @wonders8410
    @wonders8410 Місяць тому +2

    Hey just want to let you know that there is an error in the code that you have presented if you do 7+ =you know it equals an error but that error is a text and you can do something like Error + 7 = FUNCTION ERROR() {[NATIVE CODE]}

  • @mirmumit3888
    @mirmumit3888 4 місяці тому

    Can u plz tell me, how to change the only color of the text "Error" on the calculator display?

  • @slender2128
    @slender2128 5 місяців тому

    Good Video ❤🎉

  • @zzumiittow
    @zzumiittow 3 місяці тому

    In my case, the JavaScript code only works if I declare the const “display” inside the appendToDisplay function, why is that? I tried using the exact same code and it didn’t work, that doesn’t make sense.

  • @Oh_Sayang
    @Oh_Sayang 5 місяців тому +1

    Now my iPad can finally have a calculator app lol

  • @nurialsaeed5769
    @nurialsaeed5769 6 місяців тому

    great

  • @FadedLeonidas
    @FadedLeonidas 5 місяців тому

    entertaining af

  • @RahulBhatia-py1iv
    @RahulBhatia-py1iv Місяць тому

    U are legend 😅mann

  • @pastori2672
    @pastori2672 6 місяців тому +2

    how would you implement stuff like factorial and log etc.. ?

    • @thecoolnewsguy
      @thecoolnewsguy 6 місяців тому +2

      For log, use the built-in Math object, for factorial, look for an algorithm on the internet or a library

  • @FijiAura_GD
    @FijiAura_GD 6 місяців тому

    Hi Bro Code, can you do a Lua full course?

  • @shauryacode76
    @shauryacode76 3 місяці тому +1

    Can you please tell me why my clear display isn’t working

  • @delhiwarriorgaming2882
    @delhiwarriorgaming2882 5 місяців тому +1

    sir, i am also practicing that code but I have a problem with the clear button, when I am clicking it nothing happens.....pls solve it..

  • @user-yl2lp9ey1x
    @user-yl2lp9ey1x 3 місяці тому

    thank you for this video....mine is showing "ncaught ReferenceError: appendToDisplay is not defined"

  • @user-ej4vt2vg4o
    @user-ej4vt2vg4o 3 місяці тому

    you know that your the best yeah

  • @crackllitos1577
    @crackllitos1577 3 місяці тому

    This video was very helpful, but is there a way were I can make the calculator into mobile mode?

  • @cheaterlp23
    @cheaterlp23 Місяць тому

    My version didn't work, so I searched for a while.
    Guess what the error was.
    Correct:
    My version:
    =_=
    Anyway, really useful video, learned a lot.

  • @darkslopegaming6855
    @darkslopegaming6855 6 місяців тому +1

    Do you have a patreon or something? I would love to support you!

  • @kapteinoliver
    @kapteinoliver 4 місяці тому

    👏👏👏👏

  • @ForuXe
    @ForuXe 3 місяці тому +1

    When i click on numbers, nothing appears on display. I checked several times, what could it be ?

  • @jamesdillard4114
    @jamesdillard4114 2 місяці тому

    what could we use instead of eval?

  • @oluwafemiadesope2815
    @oluwafemiadesope2815 Місяць тому

    My appendToDisplay function doesn’t show my values when I click on them, any ideas what’s wrong?

  • @strategistaow3520
    @strategistaow3520 5 місяців тому

    Calculator is easy thing
    Can you make 3d space like in games?

  • @moughkeracaleb3837
    @moughkeracaleb3837 5 місяців тому

    welldone @BroCode same is not working for me......thanks for the good work.

  • @fergthediddly3238
    @fergthediddly3238 2 місяці тому

    How did you open another tab i.e html tab???

  • @theuser810
    @theuser810 4 місяці тому

    How did you get the setup where your website autosyncs with the code? That seems like a lifesaver!

    • @danarseptiyanto4066
      @danarseptiyanto4066 4 місяці тому +3

      It's "live server" extension on VS Code, it auto refresh the browser whenever you save, very useful

    • @theuser810
      @theuser810 4 місяці тому

      @@danarseptiyanto4066 Thanks

  • @hada31
    @hada31 Місяць тому

    function calculate() {
    try {
    display.value = math.evaluate(display.value);
    } catch (error) {
    display.value = "Error";
    }
    }

  • @tjarrows14
    @tjarrows14 2 місяці тому

    Just curious how you get your project to update in ream time? I have to stop it and rerun it every time to check out a new change I made in the css. I use visual code studio as well. Thank you.