-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
119 lines (98 loc) · 3.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//Current line
var CurrentId = undefined;
var inputValues = [];
const inputPrompts = [
"How much tip would you like to give? 10, 12, or 15? ",
"How many people to split the bill?",
];
let isFirstClick = true;
//Click Run
$(document).ready(function () {
$("#run-button").click(function () {
inputValues = []
$("#Content").empty();
NewLine("Welcome to the tip calculator!", false);
NewLine("What was the total bill? $", true);
});
});
//Enter button
$(document).on("keydown", function (e) {
var x = event.which || event.keyCode;
if (x === 13 || x == 13) {
var consoleLine = $("#" + CurrentId + " input").val();
console.log(consoleLine);
inputValues.push({ id: CurrentId, val: consoleLine });
console.log(inputValues);
if (inputValues.length > inputPrompts.length) {
console.log("called");
const bill = Number(inputValues[0].val);
const tip = Number(inputValues[1].val);
const people = Number(inputValues[2].val);
const tip_as_percent = tip / 100;
const total_tip_amount = bill * tip_as_percent;
const total_bill = bill + total_tip_amount;
const bill_per_person = total_bill / people;
let final_amount = Number(Math.round(bill_per_person * 100) / 100);
if (final_amount % 1 == 0) final_amount = `${final_amount}.00`;
else if ((final_amount * 10) % 1 == 0) final_amount = `${final_amount}0`;
NewLine("Each person should pay: $" + final_amount, false);
$(".console-carrot").remove();
return;
}
$(".console-carrot").remove();
NewLine(inputPrompts[inputValues.length - 1], true);
// setTimeout(NewLine, delay);
}
});
$(document).on("keydown", function (e) {
var x = event.which || event.keyCode;
var line = $("#" + CurrentId + " input");
var length = line.val().length;
if (x != 8) {
line.attr("size", 1 + length);
} else {
line.attr("size", length * 0.95);
}
if (length === 0) {
$("#" + CurrentId + " input").attr("size", "1");
}
});
$(document).on("click", function (e) {
$("#" + CurrentId + " input").focus();
});
//New line
function NewLine(text, isPrompt) {
if (CurrentId !== undefined) {
$("#" + CurrentId + " input").prop("disabled", true);
}
CurrentId = "consoleInput-" + GenerateId();
if (isPrompt) {
$("#Content").append(
//One Line
'<div id="' +
CurrentId +
'">' +
text +
'<input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" type="text" class="terminal-input" /><div class="console-carrot"></div></div>'
);
$("#" + CurrentId + " input").focus();
$("#" + CurrentId + " input").attr("size", "1");
} else {
$("#Content").append('<div id="' + CurrentId + '">' + text + "</div>");
}
}
function GenerateId() {
return Math.random().toString(16).slice(2);
}
`print("Welcome to the tip calculator!")
bill = float(input("What was the total bill? $"))
tip = int(input("How much tip would you like to give? 10, 12, or 15? "))
people = int(input("How many people to split the bill?"))
tip_as_percent = tip / 100
total_tip_amount = bill * tip_as_percent
total_bill = bill + total_tip_amount
bill_per_person = total_bill / people
final_amount = round(bill_per_person, 2)
# FAQ: How to round to 2 decimal places?
# Find the answer in the Q&A here: https://www.udemy.com/course/100-days-of-code/learn/lecture/17965132#questions/13315048
print(f"Each person should pay: final_amount")`;