Created
November 17, 2016 10:01
-
-
Save barakplasma/04658ea288b2ecdb047f66d425c95808 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//By Michael Salaverry | |
//MichaelS@wix.com | |
//feature toggles at the end of the file for code reuse | |
/* Assignment Text | |
Week 1: | |
Please hold the whole assignment in the same file, and just comment out the last one you've completed. Repeat all parts necessary so that the assignment can run just be un-commenting a whole section | |
Do not use arrays. | |
1 - Number guessing game | |
a. Write a program that asks the user to guess a number, and then say if it was lower, higher, or right on. | |
b. Extend the program, so now the user has 2 attempts to guess the same number. If he gets it on the first attempt, no need (of course) to ask again. | |
c. Extend it to 3 times | |
d. Now generate a random number instead of holding the number "hard coded". Google how to randomize it. | |
2. General exercises | |
a1. Naive cashier - ask the user how much a product costs, then ask him how many products he bought. Tell him how much it costs: for example: for 5 items that cost 4, the program will print 20. | |
a2. Assume that A was in shekels, now add the sum in dollars too (hold a local variable for the shekels to usd rate and do the math). | |
b. Ask the user for 3 numbers. Print the highest of the 3. | |
c1. Simple calculator - ask the user for 2 numbers, then print their sum | |
c2. Simple calculator - ask the user for 2 numbers, then print their subtraction | |
c3. Now ask the user for 2 numbers, and then ask him for the operation he wants to do. Support adding, substracting, dividing and multiplying. | |
c4. Use a "switch"(http://lmgtfy.com/?q=javascript+switch+case) case for c3, in case you've used 3 "ifs". | |
d. Get the name of the user, and return it's initials. For example: for 'Bob Davison', print 'BD'. Involves little things we did not discuss. Again, google is your friend. | |
*/ | |
//1.a beginning | |
/* | |
var number = prompt('Guess a number'); | |
if (number > 42) { | |
console.log('Too high!'); | |
} else if (number < 42){ | |
console.log('Too low!'); | |
} else{ | |
console.log('Right on!') | |
} | |
*/ | |
//1.b&c begginng (Example only) | |
var actualNumber, tries = 0; | |
function runOneBC(maxTries,target){ | |
if(Number.isInteger(target) === true){ | |
actualNumber = target; | |
}else{actualNumber = Math.floor(Math.random() * 100);} | |
while (highlowhit(number) !== 2 && tries < maxTries){ | |
var number; | |
number = prompt('Guess a number'); | |
number = parseInt(number); | |
if(Number.isInteger(number) === true){ | |
printResult(highlowhit(number)); | |
}else{ | |
throw "Not an Integer"; | |
} | |
tries++; | |
} | |
} | |
function highlowhit(number){ | |
if (number > actualNumber) { | |
return 0; | |
} else if (number < actualNumber){ | |
return 1; | |
} else if (number == actualNumber){ | |
return 2; | |
} else{ | |
console.Error; | |
} | |
} | |
function printResult(result){ | |
switch(result){ | |
case 0: | |
alert('Too high!'); | |
break; | |
case 1: | |
alert('Too low!'); | |
break; | |
case 2: | |
alert('Right on!') | |
break; | |
default: | |
console.Error; | |
} | |
} | |
//2 | |
//Naive Cashier | |
function Register (){ | |
this.pp = 1; | |
this.qp = 1; | |
this.currency = 'nis'; | |
this.exchangeRate = 3.8; | |
this.subtotal = 1; | |
this.total = 1; | |
} | |
Register.prototype.regKeypad = function () { | |
this.pp = prompt("Product Price?"); | |
this.qp = prompt("Quantity?"); | |
this.currency = prompt("Currency Symbol? usd or nis"); | |
} | |
Register.prototype.CalculateSubTotal = function (){ | |
this.subtotal = this.pp * this.qp; | |
} | |
Register.prototype.RegisterDisplay = function (){ | |
alert(this.currency + " " + this.total); | |
} | |
Register.prototype.exchange = function (){ | |
if(this.currency == 'nis'){ | |
this.total = this.subtotal * this.exchangeRate; | |
} else{ | |
this.total = this.subtotal; | |
} | |
} | |
function run2a(){ | |
var reg = new Register(); | |
reg.regKeypad(); | |
reg.CalculateSubTotal(); | |
reg.exchange(); | |
reg.RegisterDisplay(); | |
} | |
//2b b. Ask the user for 3 numbers. Print the highest of the 3. | |
function run2b(){ | |
this.n1 = Calculator.buttonPress(); | |
this.n2 = Calculator.buttonPress(); | |
this.n3 = Calculator.buttonPress(); | |
console.log(Math.max(this.n1,this.n2,this.n3)); | |
/* | |
var notAnArray = []; | |
for(i=0;i<3;i++){ | |
notAnArray[i] = Calculator.buttonPress(); | |
} | |
notAnArray.sort(); | |
console.log(notAnArray[2]); | |
*/ | |
} | |
var Calculator = { | |
n1 : 2, | |
n2 : 4, | |
result : 0, | |
buttonPress : function() { | |
return parseInt(prompt("Please input a number :")); | |
}, | |
calcKeypad : function() { | |
Calculator.n1 = Calculator.buttonPress(); | |
Calculator.n2 = Calculator.buttonPress(); | |
}, | |
add : function(){ | |
Calculator.result = Calculator.n1+Calculator.n2; | |
}, | |
sub : function(){ | |
Calculator.result = Calculator.n1-Calculator.n2; | |
}, | |
div : function(){ | |
Calculator.result = Calculator.n1/Calculator.n2; | |
}, | |
mul : function(){ | |
Calculator.result = Calculator.n1*Calculator.n2; | |
}, | |
ask : function(){ | |
choice = prompt("Which Operation? +,-,/,*"); | |
switch(choice){ | |
case "+": | |
Calculator.add(); | |
break; | |
case "-": | |
Calculator.sub(); | |
break; | |
case "/": | |
Calculator.div(); | |
break; | |
case "mul": | |
Calculator.mul(); | |
break; | |
default: | |
console.log("invalid input"); | |
break; | |
} | |
}, | |
calcOut : function(){ | |
alert("Result: "+Calculator.result); | |
} | |
}; | |
function run2c(){ | |
Calculator.calcKeypad(); | |
Calculator.ask(); | |
Calculator.calcOut(); | |
} | |
/* //Test Suite for run2c() | |
Calculator.add(); //add assert statements here | |
console.log(Calculator.result); | |
console.log(Calculator.n1 +" "+Calculator.n2); | |
*/ | |
function run2d(){ | |
this.name = prompt("Full Name please:"); | |
//testing value | |
//this.name = "Michael Salaverry" | |
this.nameArr = this.name.split(" "); | |
this.initials = this.nameArr[0].charAt(0) + this.nameArr[1].charAt(0); | |
alert(this.initials); | |
//this.initials should be "MS" | |
} | |
//http://www.jslint.com/ is cruel to this code | |
//Feature Toggles | |
//runOneBC(3,42); | |
//run2a(); | |
//run2b(); | |
//run2c(); | |
//run2d(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment