Chapter 2 Exercise Set 1 Solutions

abs(n)

function abs(n) {
    if (n >= 0)
        return n;
    return -n;
}

show(abs(-144));

makeQuestion(oprd1, oprd2, opr)

function makeQuestion(oprd1, oprd2, opr) {
    return "What is " + oprd1 + " " + opr + " " + oprd2 + "?";
}

randomInt(low, high)

function randomInt(low, high) {
    return Math.floor((high - low + 1) * Math.random() + low)
}

What is oprd1 + oprd2?

function randomInt(low, high) {
    return Math.floor((high - low + 1) * Math.random() + low)
}

function makeQuestion(oprd1, oprd2, opr) {
    return "What is " + oprd1 + " " + opr + " " + oprd2 + "?";
}

var low = 1;
var high = 10;

var op1 = randomInt(low, high);
var op2 = randomInt(low, high);

var question = makeQuestion(op1, op2, "+");
var answer;

while (true) {
    answer = prompt(question);
    if (answer == opr1 + opr2) {
        alert("You must be a genius or something.");
        break;
     }
     else if (answer == opr1 + opr2 + 1 || answer == opr1 + opr2 - 1)
         alert("Almost!");
     else
         alert("You're an embarrassment.");
}

evaluateExpression(oprd1, oprd2, op)

function evaluateExpression(oprd1, oprd2, op) {
    if (op == "+")
        return oprd1 + oprd2;
    else if (op == "-")
        return oprd1 - oprd2;
    else if (op == "*")
        return oprd1 * oprd2;
    else if (op == "/")
        return oprd1 / oprd2;
    else
        return NaN;
}

randomOp()

function randomOp() {
    var opnum = randomInt(1, 4);

    if (opnum == 1)
        return "+";
    else if (opnum == 2)
        return "-";
    else if (opnum == 3)
        return "*";
    else
        return "/";
}

What is oprd1 [+ | - | * | /] oprd2?

function randomInt(low, high) {
    return Math.floor((high - low + 1) * Math.random() + low)
}

function makeQuestion(oprd1, oprd2, opr) {
    return "What is " + oprd1 + " " + opr + " " + oprd2 + "?";
}

function randomOp() {
    var opnum = randomInt(1, 4);

    if (opnum == 1)
        return "+";
    else if (opnum == 2)
        return "-";
    else if (opnum == 3)
        return "*";
    else
        return "/";
}

function evaluateExpression(oprd1, oprd2, op) {
    if (op == "+")
        return oprd1 + oprd2;
    else if (op == "-")
        return oprd1 - oprd2;
    else if (op == "*")
        return oprd1 * oprd2;
    else if (op == "/")
        return oprd1 / oprd2;
    else
        return NaN;
}


var low = 1;
var high = 10;

var op1 = randomInt(low, high);
var op2 = randomInt(low, high);
var op = randomOp();

var question = makeQuestion(op1, op2, op);
var correct = evaluateExpression(op1, op2, op);
var answer;

while (true) {
    answer = prompt(question);
    if (answer ==  correct) {
        alert("You must be a genius or something.");
        break;
     }
     else if (answer == correct + 1 || answer == correct - 1)
         alert("Almost!");
     else
         alert("You're an embarrassment.");
}

splitCheck(amt, numPeople, tip)

var check;

splitCheck on the dollar

var check;

greaterThan(n)

function greaterThan(x) {
    return function(y) {
               return y > x;
            };
}

var greaterThanTen = greaterThan(10);
alert(greaterThanTen(9));