Build a Calculator with Vue.js

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • In this tutorial, you will learn to use Vue.js to create a basic calculator component inside a web browser.
    💻Code: github.com/cod...
    🔗Check out Cody Seibert's main channel: / @webdevcody
    --
    Learn to code for free and get a developer job: www.freecodeca...
    Read hundreds of articles on programming: medium.freecod...

Komentáře • 141

  • @a_rob
    @a_rob Před 5 lety +36

    Hi, in the equal function 'this.previous' should be the first and 'this.current' should be the second argument
    equal() {
    this.current = `${this.operator(parseFloat(this.previous), parseFloat(this.current))}`
    this.previous = null
    }
    otherwise, minus operator will give you inverted results.
    thank you for you great work.

    • @tinaj1323
      @tinaj1323 Před rokem

      Thanks, now is my equal function perfekt!!

  • @RobertHolak
    @RobertHolak Před 6 lety +55

    In your equal() method you need to switch the order of current and previous. Otherwise 10 / 5 comes out to 0.5 instead of 2. Aside from that little logic bug, this was really helpful since I am learning Vue at the moment myself.
    [Update] I was going to make a pull request but there's already one there for this bug.

    • @zraffens5145
      @zraffens5145 Před 4 lety +4

      Random question: How'd you get verified just with 3 subscribers?

  • @Romeo-vt8hq
    @Romeo-vt8hq Před 3 lety +4

    Instead of typing a string with number each time you can do
    4
    and then
    type(e) {
    this.result += parseInt(e.target.innerHTML);
    },

  • @alexcubed4270
    @alexcubed4270 Před 6 lety +16

    21:13 “50 divided by 4 equals 0.08”
    You switched the a and b in the calling the operator function. A should be previous and b should be current

    • @JamesMarks
      @JamesMarks Před 6 lety

      Seconded

    • @_ifly
      @_ifly Před 4 lety

      did you have solved this problem can you share.

    • @alexcubed4270
      @alexcubed4270 Před 4 lety +4

      @@_ifly
      equal() {
      this.current = `${this.operator(
      parseFloat(this.previous),
      parseFloat(this.current)
      )}`;
      this.previous = null;
      }

    • @malamhari_
      @malamhari_ Před 4 lety +1

      @@alexcubed4270 thanks

    • @jamlizzy101
      @jamlizzy101 Před 3 lety +1

      Totally agree

  • @AccessCode101
    @AccessCode101 Před 4 lety +5

    Wow, this made my vue learning journey a lot more enjoyable. I didn't know we could do these things.

  • @SilvetaGui
    @SilvetaGui Před 5 lety +1

    Very nice tutorial. However, the part who took my attention was definitely your fluency using your editor.
    It even made me question if I should make the switch from IntelliJ to VSCode.

  • @RandallWhite434
    @RandallWhite434 Před 5 lety +1

    I think a good next step would be to show how this calculator could be made into a component for nesting into a larger project. Thanks for the video.

  • @cigomba
    @cigomba Před 3 lety

    Thanks for sharing. This is the easiest implementation of a calculator webapp that I have found so far and I was able to follow it throughly even though it was my first time using Vue.js. Thanks again!

  • @EnricoDeleoOfficial
    @EnricoDeleoOfficial Před 6 lety +5

    I would have based the calculator on numbers not strings. When I saw the sign() method implemented with character comparison I fell down from my chair :) the right way of switching sign of any number is to multiply it by -1 so it should be this.current * -1

  • @eyadbereh
    @eyadbereh Před 3 lety

    I don't really care about what this calculator is missing, the most important objective is to understand the concept here, and i think this video pretty much explains it
    Thanks for the great explanation

  • @nlt152
    @nlt152 Před 6 lety +35

    css grid is awesome!!!!!!!!!!!

    • @kamalhm-dev
      @kamalhm-dev Před 6 lety +2

      flexbox is better, fight me

    • @nachnamevorname9801
      @nachnamevorname9801 Před 6 lety +1

      isnt flexbox just a css grid with one row/column?

    • @Ipkpjersi
      @Ipkpjersi Před 6 lety +7

      Flexbox and CSS Grid are supposed to be used together, they are not competitors.

  • @i_youtube_
    @i_youtube_ Před 6 lety +2

    Awesome tutorial as a simple overview on how vue.js works. Thank you.

  • @ChiakiNanami736
    @ChiakiNanami736 Před 6 lety +12

    I understand skipping setup in the video itself, but giving the options used for the Vue CLI in the video's description would have been nice.

    • @Ipkpjersi
      @Ipkpjersi Před 6 lety +2

      He still hasn't given them. Lame.

    • @dabhiridaniel847
      @dabhiridaniel847 Před 5 lety

      make sure you have node js installed then use cmd to install : npm install -g vue-cli

  • @krave96
    @krave96 Před 6 lety +4

    Hello! You've made a great and simple video. But there's a bug that you don't show on video - if you want to type a new number after calculating, it appends to the result instead of clearing the display)) I fixed it by adding a new variable "Result" to data() and a condition to append() method.
    Thank you for the video, please continue to do such tutorials. I can suggest you to make an audio player using HTML5 audio and Vue.js

    • @_ifly
      @_ifly Před 4 lety

      great job it solved the division problem too. before this when i was dividing 8 /4 = 0.4 now ok

  • @kidbrave_7673
    @kidbrave_7673 Před 2 lety

    Great video, I'm just checking this out a a year later trying to find out if I want to learn react or vue and so far vue looking really fun to start building web applications. 😁🙌🏽

  • @ki85squared
    @ki85squared Před 6 lety +24

    Implementing a calculator with strings? That's a bold move, Cotton...

  • @AwesomeAdijos
    @AwesomeAdijos Před 6 lety +5

    we want more projects with vue :D

  • @miroslavmakhruk4102
    @miroslavmakhruk4102 Před 5 lety +4

    You should switch previous and current in your equal method. 50 divided by 4 is 0.08 - it should've rang a bell haven't it?

  • @00el04
    @00el04 Před 3 lety +1

    awesome! keep building more vue apps !

  • @zinaright
    @zinaright Před 4 lety

    Sold to VueJs, finally! Thanks

  • @raymondpcspangcheeseng7410

    thx for giving the code and sharing this calculator

  • @blackblock7583
    @blackblock7583 Před 6 lety

    you can enter +/- (I am on Windows) on mac by shift pressing key under escape.

  • @enginedavey
    @enginedavey Před 6 lety

    Another suggestion could be to enhance the app to use vuex to show state management in vue. By the way great video. I built a similar app using vanilla js and it took me a lot longer than 22 mins. Vue is great!

  • @jannovaczek3927
    @jannovaczek3927 Před 2 lety

    Hi, avesome tutorial. Thanks.

  • @TheSETJ
    @TheSETJ Před 6 lety

    You've developed a calculator in 20 minutes, that's awesome. Thanks.
    BTW, it's good if you create another video, developing the same calculator completely, and going into details and best practices.

  • @diego.almeida
    @diego.almeida Před 3 lety +1

    Great video! I'm just starting with Vue.js and was wondering what is the criteria for deciding how to divide an app into reusable components. For example, could I create a Vue component for the keys called keyComponent and each key of the calculator would be an instance of this Vue component?

  • @Deliverant
    @Deliverant Před 6 lety +4

    Hey cody thanks for the video, do you have any plan on a new big Fullstack project like the one you did with vue+express? This time maybe with nuxt, would be the best thing ever

  • @ruspinaspark2549
    @ruspinaspark2549 Před 5 lety +2

    Thanks for your informative video
    in sign method you can multiple this.current *-1 :)

  • @user-go8cr9wz4z
    @user-go8cr9wz4z Před 4 lety

    yeah ,this is good example for VUE,thank you buddy!

  • @saurabhhchavan
    @saurabhhchavan Před 3 lety

    Awersome tutorial ❤️ we want more vuejs project ☺️

  • @Romeo-vt8hq
    @Romeo-vt8hq Před 3 lety +1

    My solution for checking whether the dot exists
    // Append Dot only if it doesn't exist
    dot(e) {
    let symbol = e.target.innerHTML;
    if(!this.result.includes(symbol)) {
    this.result += e.target.innerHTML;
    }
    }

  • @hiyayacko
    @hiyayacko Před 6 lety

    Thank you for sharing. This video is great for learning Vue.js!

  • @vincentyoumans
    @vincentyoumans Před 4 lety

    Good job... how about some videos on doing Menu's and Routing. Ideally vue.js . single page . where we just slide a card out of view and transition a new card for its place.

  • @KenKopelson
    @KenKopelson Před 5 lety +4

    Well, first of all, thanks for making this tutorial. I wanted to offer some helpful advice if I may. This is not meant to attack you, or to minimise the nice job you did in presenting the material. What I'd like to offer is the thought that perhaps it would have been much simpler and easier to understand if you had used a "number" data type for a "total" field, and also a "number" data type for the "current" field, which is the current field being entered. It doesn't seem that a "string" type is the right type for a calculator, which is meant to calculate numeric values.
    For example, if you had made the variable a number, then you could have done the '%' function as: "this.total /= 100.0", and that would be it. When a person presses an operator, you would store the operation into a variable, (such as addition for '+'), and then before you change to a new operator you resolve the current one. You would have to implement a calculation stack if you wanted to support proper operator precedence. You could also have a "total" variable that contains the current total, which is different from the "current" value being entered. To add a new digit to the present "current" you could then do the formula (current * 10 + digit). By doing this, you avoid all this parsing of strings. Javascript is capable of displaying number values easily, either with floating point or with fixed point.
    i hope this was helpful.

  • @saelfaer
    @saelfaer Před 6 lety +1

    what's up with the operator clicked state... why does the append method need to check that. your setPrevious could just as well reset current instead of setting an operator clicked flag no?

  • @nihinlola
    @nihinlola Před 5 lety

    In your equal function, the positions of this.previous and this.current should be switched, else the calculation would not be correct.

  • @stevenkennedy8609
    @stevenkennedy8609 Před 6 lety

    Great intro video, thank you! Will be sharing

  • @RobertMion
    @RobertMion Před 6 lety

    For anyone looking to learn more Vue by making the iOS calculator a completely different way, here's a tutorial I wrote on Medium. Bonus: it uses Grid **and** Flexbox: medium.com/@rmion/learn-vue-css-grid-and-flexbox-10-part-beginner-tutorial-20f8c422ea06

  • @IsraelCena
    @IsraelCena Před 6 lety

    Thanks for demonstrating

  • @helshabini
    @helshabini Před 6 lety

    CSS grid is a god send.

  • @dabhiridaniel847
    @dabhiridaniel847 Před 5 lety +1

    A little bug but can be fixed....just update the equal function : `${this.operator(
    parseFloat(this.previous),
    parseFloat(this.current)
    )}` ; ..............this.previous should come first not the otherwise

  • @mariusselvfolgelig
    @mariusselvfolgelig Před 5 lety +2

    this.current *= -1; would have been a few less characters in the sign method (If it had been a number of course :P)

    • @IanSebryk
      @IanSebryk Před 3 lety

      this still works just fine, just with a small tweak: `${parseFloat(this.current) * -1}`. ironically, what he did for percentage was right on the money, logic-wise.

  • @anroopvullampathi
    @anroopvullampathi Před 5 lety

    Hii ,
    Thanks for putting calculator example, pls can u keep calendar example...............

  • @prckomir
    @prckomir Před 5 lety

    this is only ok if you are operating 2 numbers.. for example 1+2+3 will be 5

  • @LukiausdemKaff
    @LukiausdemKaff Před 4 lety

    Hi, thanks for the video. What editor are you using? Thanks in advance.

  • @taslimoseni2231
    @taslimoseni2231 Před 5 lety +2

    Had to reduce the speed to 0.5 so I could catch up, nice video though!

  • @cliffe_rocker
    @cliffe_rocker Před 4 lety

    This is brilliant.

  • @AbhishekKumar-mq1tt
    @AbhishekKumar-mq1tt Před 6 lety

    Thank u for this awesome video

  • @user-ru8bq6vl5t
    @user-ru8bq6vl5t Před 6 lety

    thanks you for sharing

  • @TheRocreex
    @TheRocreex Před 6 lety +16

    Wait, so 50/4 = 0.08?

    • @jeremyblake
      @jeremyblake Před 5 lety

      his divide and subtract is messed up. 3 - 2 will return -1.

    • @dabhiridaniel847
      @dabhiridaniel847 Před 5 lety +2

      A little bug but can be fixed....just update the equal function : `${this.operator(
      parseFloat(this.previous),
      parseFloat(this.current)
      )}` ; ..............this.previous should come first not the otherwise

  • @mr.yellow3250
    @mr.yellow3250 Před 4 lety

    why I cant click the divs as he did ? I added my click function but it doesnt work. Should I replace those divs to button ?

  • @iNVICTUX
    @iNVICTUX Před 5 lety

    Thanks!

  • @scorpionedge
    @scorpionedge Před 4 lety

    didn't anyone notice the critical bug with this is that if you press 1 + 1 + 1 and then press equal, you get 2..

  • @KarlOlofsson
    @KarlOlofsson Před 6 lety

    I found it confusing to have a common operator function that switches between lambda functions to do the correct calculation at use, I did not catch that design on the first watching. Any particular reason you choose that design since you have to have different functions for each operand anyway to set them, besides simplifying the "equals" method?

  • @user-rx7st2do6p
    @user-rx7st2do6p Před 6 lety

    Cool, I love grid.

  • @kimcodemonkey
    @kimcodemonkey Před 6 lety

    awesome. thanks for sharing..

  • @groce147
    @groce147 Před 5 lety

    thanks man

  • @stephenjayh
    @stephenjayh Před 4 lety +5

    I love the vid but feel that this is more of a JS / CSS tutorial lol
    Why didn’t you separate the numbers into separate components to show off Vue’s reusable component system?

  • @steverichter1546
    @steverichter1546 Před 5 lety

    that was outstanding.

  • @boazcstrike
    @boazcstrike Před 6 lety

    Thank you sooo much! :)

  • @st1m965
    @st1m965 Před 6 lety

    thanks very interesting

  • @harrisonfok8267
    @harrisonfok8267 Před 3 lety

    I don't really understand the operator part. How does it know what a and b are?
    this.operator = (a, b) => a * b

    • @simonzeman6876
      @simonzeman6876 Před 2 lety

      Watch equal() function again. This operator stores a function and you provide parameters in equal()

  • @Sefelizrealmente222
    @Sefelizrealmente222 Před 5 lety

    Do you have a tutorial that show how to set up everything before working with the code part?

    • @BunrakBjj
      @BunrakBjj Před 5 lety

      Pretty much all you need is: node and npm, then on visual studio code terminal do
      1) npm install -g vue-cli
      2) vue init webpack nameofproject
      -respond to the (y/n)
      3) cd nameofproject
      4) npm run dev
      This should bring you to the start of this video

  • @spliterr
    @spliterr Před 2 lety

    1:1 error Component name "Calculator" should always be multi-word vue/multi-word-component-names
    why this is happening and how do i fix this? ty

    • @youcef476
      @youcef476 Před rokem

      multiword the name must has two for exemple CoolCalculator the name of component has minmum 2 words which are cool and calculator and cool-component same
      is good

  • @AishahYusra
    @AishahYusra Před 4 lety

    how do u change multiple div class names at same time , or how to select multiple div and put same class name at the same time ....

    • @eduardor.romero3900
      @eduardor.romero3900 Před 3 lety

      With shortcuts. Holding alt key you can put multiple cursors. Ctrl D for selecting multiple words. Try ctrl alt arrows and shift alt arrows

  • @Shinxbi__
    @Shinxbi__ Před 23 dny

    06:25 what kind of black magic did he do here???

  • @oinn6216
    @oinn6216 Před 4 lety

    append('1') - why no to get event.target.innerText ?

  • @cynicalobserver1118
    @cynicalobserver1118 Před 4 lety +1

    Presenting at 1.25x speed by default. I like that.

  • @TheShadetheory
    @TheShadetheory Před 5 lety

    11:15 I would simply write that as `this.current -= (this.current * 2)`

    • @pikachu-fe7tx
      @pikachu-fe7tx Před 5 lety +1

      His code was more complex because of his somewhat questionable decision to implement the calculator using strings

  • @Kumar-pw1dm
    @Kumar-pw1dm Před 4 lety

    Try to make a tutorial on react native has frontend and Laravel has backend, no good tutorial found in online

  • @devIsAjourney
    @devIsAjourney Před 6 lety

    awesome video

  • @marcusaureliusregulus2833

    Good for displaying vue, but terrible ui/ux

  • @Antnix732
    @Antnix732 Před 4 lety

    I’m new to vue.js but this kinda is a JavaScript tutorial. I mean like is this how vue.js is?

  • @haytham6776
    @haytham6776 Před 5 lety

    is it require to learn typescript for vue ?

  • @KillerInnocentx
    @KillerInnocentx Před 3 lety

    I love you so much

  • @muhammadyusoffjamaluddin

    Why my calc cant event revert back when i am pressing the 'sign' button like your's?
    10:50

  • @luzaw4957
    @luzaw4957 Před 6 lety

    Next video, Chess app with Vue? :D

  • @kosemekars
    @kosemekars Před 4 lety +1

    in 20 minutes!

  • @putude
    @putude Před 5 lety

    Damm you are genius!

  • @lindaojo2
    @lindaojo2 Před 4 lety

    I really just wanna know how to edit multiple lines specifically like he did here 18:44

    • @SunnyKumar-ds5gh
      @SunnyKumar-ds5gh Před 4 lety +2

      That was VSCode function. If you are on Mac press option button and click on places where you want your cursors to be. If you want to edit the same lines in the entire file then press command+D to multiple select the similar lines of code.

    • @BarrieBM
      @BarrieBM Před 4 lety

      hold alt and press where you wanna edit, but seeing this comment was made months ago you prolly figured it out xD

    • @lindaojo2
      @lindaojo2 Před 4 lety

      😂 I figured it out today on my mac, pressing shift'option worked. Thanks

  • @naspy971
    @naspy971 Před 4 lety

    Unfortunately with this project we use very few of Vue features.

  • @crazycris4
    @crazycris4 Před 5 lety +1

    cursor: pointer

  • @ludkamotylikova3060
    @ludkamotylikova3060 Před 3 lety

    Its quite difficult. At least for me.

  • @jaredrubin219
    @jaredrubin219 Před 6 lety

    What IDE is that? Anybody??

    • @dimgg5399
      @dimgg5399 Před 6 lety +2

      VS Code, it's extremely popular

    • @jaredrubin219
      @jaredrubin219 Před 6 lety

      Thanks for the quick reply! Yeah, looks totally awesome.

  • @jurgentgjeloshaj
    @jurgentgjeloshaj Před 5 lety +2

    < great video

  • @ekshivbhakt2417
    @ekshivbhakt2417 Před 5 lety

    '-${this.current}' not working

    • @glenncott
      @glenncott Před 5 lety

      easy mistake to make. This is the grave accent character ` (the key to the left of the 1 key) not the apostrophe '

    • @schiban_
      @schiban_ Před 2 lety +1

      @@glenncott 2 years later, still being helpful. Thanks!

  • @maxron6514
    @maxron6514 Před 2 lety

    How do you know someone’s using Apple? They’ll tell you.

  • @leonpilot737
    @leonpilot737 Před 5 lety

    Just made a sample calculator using VueJs and Vuex.
    github.com/leon737/Calculator

  • @dm0nic4L
    @dm0nic4L Před 6 lety

    How does he make it a standalone app without hosting it on the browser @ 4:28?

    • @TheSETJ
      @TheSETJ Před 6 lety

      It's Mac's calculator app.

  • @pepijn1231
    @pepijn1231 Před 3 lety

    50/4 = 0.8 🧐

  • @mustaphaabdulmujeeb8980

    This application in cute

  • @jeremyleachcodingmofo
    @jeremyleachcodingmofo Před 5 lety

    IF YOU ARE LIKE HEY WHY NO CLI INSTRUCTIONS::: HERE THEY ARE. FOR LATEST... CLI v3
    In console/terminal, such as ConEmu or XTerm, Terminator, Guake, ITerm, Hyper
    [anywhere on pc]
    npm install -g @vue/cli
    [somewhere on pc like /code]
    vue create --default hello-world
    cd hello-world
    Open the folder with your code editor.
    With VS Code you can type:
    code .
    In the location you want to open.
    Probably most that support “Folders as projects” do this.

  • @PornstarCentral
    @PornstarCentral Před 5 lety

    Is it me or does he sound like Seth rollins?

  • @videosostv6065
    @videosostv6065 Před 6 lety

    AVE JS))

  • @gtbaba123
    @gtbaba123 Před 2 lety

    Without telling how to setup is useless for beginners. Even its easy but they get problems.

  • @brawndo8726
    @brawndo8726 Před 5 lety +1

    I cringed a little over all the type coercion (strings to numbers and back again)

  • @user-ct5bo7qf2j
    @user-ct5bo7qf2j Před 5 lety +1

    very bad code. sorry
    it`s good video as vue & css grid tutorial.
    However, as for the calculator algorithm - everything`s very bad

    • @jbdestiny94
      @jbdestiny94 Před 5 lety +1

      Влад Иванов
      Is that you contribution then? Can you improve the code and show to others to learn from! Thanks

  • @navi-charlotte
    @navi-charlotte Před 2 lety +1

    thumbs down because of the lame implementation of sign(). should have just multplied by -1

  • @cliffe_rocker
    @cliffe_rocker Před 4 lety

    This is brilliant.