SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Maths using randomly selected symbols

    I am randomly selecting the maths symbol (+,-,*,/) using a math.random and then applying the relevant symbol to a variable based on the result, ie if result == 1 then symbolGen = "+"

    I now need to use this symbol in the Javascript for it's original purpose, as I have two other numbers being generated and need to work out the answer.

    Many thanks in advance,

    Chris

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    What code do you have so far?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Let's go crazy and use functions to perform the operations.

    This is not how you would do it for a class where you're supposed to lean about other things, but it could be instructive in other ways.

    Code javascript:
    var add = function (a, b) {
        return a + b;
    },
        subtract = function (a, b) {
        return a - b;
    },
        multiply = function (a, b) {
        return a * b;
    },
        divide = function (a, b) {
        return a / b;
    },
        operations = [
        {symbol: '+', fn: add},
        {symbol: '-', fn: subtract},
        {symbol: '*', fn: multiply},
        {symbol: '/', fn: divide}
    ],
        i,
        type,
        operation,
        a,
        b,
        result = '';
    for (i = 0; i < 10; i += 1) {
        type = Math.floor(Math.random() * operations.length, 10);
        operation = operations[type];
        a = Math.floor(Math.random() * 10);
        b = Math.floor(Math.random() * 10);
        result += a + ' ' + operation.symbol + ' ' + b + ' = ' + operation.fn(a, b) + '\n';
    }
    window.alert(result);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    May 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly, thank you for the swift replies. Secondly, unfortunately I am not overly confident in Javascript so I am somewhat overwhelmed by your reply pmw57. The code I have so far is...

    Code:
    var firstNo = Math.floor(Math.random() * 12+1);
                var secondNo = Math.floor(Math.random() * 12 + 1);
                var mathsSymbol = "";
                var symbolGen = Math.floor(Math.random() * 4 + 1);
                var completeEquation;
                var answer;
    
                if (symbolGen == 1) {
                    mathsSymbol = "+";
                } else if (symbolGen == 2) {
                    mathsSymbol = "-";
                } else if (symbolGen == 3) {
                    mathsSymbol = "*";
                } else {
                    mathsSymbol = "/";
                }
    
                answer = firstNo + mathsSymbol.hasOwnProperty + secondNo;
    
                completeEquation = firstNo + " " + mathsSymbol + " " + secondNo + " =  " + answer;
    
                var question = document.getElementById('equation');
                question.innerHTML = completeEquation;
    Do you think you could possibly explain a method of modifying the code to incorporate your answer. It was very good but I need some help understanding it.

    Many thanks,

    Chris

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    There is only one way to move on from where you are, and that is to use the eval method to evaluate the result of a string.

    However, eval is considered evil so it is better to use other techniques.

    For example, this would work with your existing code:

    Code javascript:
    if (symbolGen == 1) {
        answer = firstNo + secondNo;
    } else if (symbolGen == 2) {
        answer = firstNo - secondNo;
    } else if (symbolGen == 3) {
        answer = firstNo * secondNo;
    } else {
        answer = firstNo / secondNo;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •