Code Monkey home page Code Monkey logo

calculator-app's Introduction

Calculator-App

A Simple Calculator App created by implementing binary operations in Swift.

Screenshot

calculator-app's People

Contributors

siddique26 avatar

calculator-app's Issues

Division Issue

For Division.

            let lhs = currentValue.index(currentValue.startIndex, offsetBy: 0)
            let  rhs = currentValue.index(currentValue.startIndex, offsetBy: 2)
//Here LHS and RHS are just indices 0 and 2 
            let a = String(currentValue[lhs])
            let b = String(currentValue[rhs])
//a is just the character in position 0 and b is the character in position 2 (typecast as strings)
            let operand1 = Double(a)
            let operand2 = Double(b)
// operand 1 is double of the a. operand 2 is double of b
//This works if the two characters are numbers. But not if they are non-numeric. 
//If string is "1/3" then a is 1 and b is 3. So Double(a) and Double(b) are valid. 
//But if String is "12/3" then a is 1 and b is /. Double(b) is Double(/) which is invalid. So nil is //returned
            answer = operand1! / operand2!
//Here you are unwrapping an optional by using !. Operand1 and Operand2 must not be nil. 
//But they will be nil if either a or b are non-numeric and cannot be converted to Double. 

Another error is that you can only divide with string of form "a/b" where a and b are two digits.
What about "aaa/b" "aa/bbb" etc etc.

The better solution is to use

let numberArray = currentValue.components(separatedBy: "/")
This returns an array of strings separated by "/". Then take the first two elements of the array and convert to double. Then divide.

  ```
    let a  = numberArray[0]
    let b  = numberArray[1]
          
    let operand1 = Double(a)
    let operand2 = Double(b)

    let answer: Double = operand1! / operand2!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.