-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08Mar.js
114 lines (95 loc) · 3.24 KB
/
08Mar.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
var resultDiv = document.getElementById("result");
var compGuess = 0;
var attempts = 10;
var prevGuess = 0;
//1. Reverse Words in a sentence & 2. Pig Latin
function reverseWords(n, pig) {
var latin = pig === true ? "ay " : " ";
var result = "";
n = n.split(" ");
if (pig) {
for (i = 0; i < n.length; i++) {
result += n[i].substr(1, n.length).concat(n[i].substr(0, 1)) + latin;
}
} else {
for (i = 0; i < n.length; i++) {
result += n[i].split("").reverse().join("") + latin;
}
}
resultDiv.innerHTML = result;
}
//3. Print number without String Methods
function numPrint(n) {
var result = [];
for (n; n > 1; Math.floor(n /= 10)) {
var digit = Math.floor(n % 10);
result.push(digit);
}
resultDiv.innerHTML = result.reverse().join("\n");
}
//4. Guessing Game
function guessGame() {
compGuess = (Math.floor(Math.random() * 100)) + 1;
resultDiv.innerHTML = "You have " + attempts + " attempts. ", "Start Guessing!"
}
function checkGuess(userGuess) {
var guessMsg = "";
if (attempts > 1) {
if (prevGuess == userGuess) {
guessMsg = "You tried this number last time, try a different one.";
} else {
if (userGuess == compGuess) {
guessMsg = "You guessed it right, the number was " + compGuess + " indeed!";
} else if (userGuess > compGuess) {
guessMsg = "Your guess was higher, try again.";
} else {
guessMsg = "Your guess was lower, try again.";
}
attempts -= 1;
prevGuess = userGuess;
}
resultDiv.innerHTML = guessMsg + "Remaining Attempts is " + attempts + ".";
} else {
resultDiv.innerHTML = "Game Over! The number was " + compGuess;
reset();
}
}
function reset() {
prevGuess = 0;
attempts = 10;
compGuess = 0;
}
function printWords(numb) {
resultDiv.innerHTML = "<p>" + printWord(numb) + "</p>";
function printWord(digit) {
var ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
var tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
var tenones = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
if (digit < 10) {
return ones[digit];
} else if (digit < 20) {
onesPlace = digit % 10;
return tenones[onesPlace];
} else if (digit < 100) {
tensDigit = Math.floor(digit / 10);
onesDigit = digit % 10;
return tens[tensDigit - 2] + ' ' + ones[onesDigit];
} else if (digit < 1000) {
hundDigit = Math.floor(digit / 100);
hundRemainder = digit % 100;
return printWord(hundDigit) + ' hundred ' + printWord(hundRemainder);
} else if (digit < 1000000) {
thouDigit = Math.floor(digit / 1000);
thouRemainder = digit % 1000;
return printWord(thouDigit) + ' thousand ' + printWord(thouRemainder);
} else if (digit < 1000000000) {
millDigit = Math.floor(digit / 1000000);
millRemainder = digit % 1000000;
return printWord(millDigit) + ' million ' + printWord(millRemainder);
} else if (digit < 1000000000000) {
billDigit = Math.floor(digit / 1000000000);
billRemainder = digit % 1000000;
return printWord(billDigit) + ' billion ' + printWord(billRemainder);
}
}
}