-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.js
61 lines (49 loc) · 1.33 KB
/
index2.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
var splitInteger = function(num, parts) {
// Complete this function
if (num < 0 || parts < 0) {
return "Please enter non-negative integers!";
}
var partsArray = [];
if (num >= parts) {
var first = parseInt(num / parts); //The first part of the number
var y = num % parts; //Number of occurrences of the latter part
console.log("y is " + y);
var x = parts - y; //Number of occurrences of the previous part
for (var i = 0; i < x; i++) {
partsArray.push(first);
}
for (var i = 0; i < y; i++) {
partsArray.push(first + 1);
}
return partsArray;
} else {
return "parts should be smaller than num!";
}
};
let answer = splitInteger(20, 6);
console.log(answer);
let another = [3, 1, 2];
console.log(...another);
console.log("another " + another.slice().sort((a, b) => b - a)); //sort array descending order
var i = 4;
while (--i) {
console.log("i is " + i);
}
var getReversed = function(theString) {
var r = "";
for (var i = theString.length - 1; i >= 0; i--) {
r += theString[i];
}
return r;
};
console.log(getReversed("Timothy"));
//or write it like this using prototypes
String.prototype.getReversed = function() {
var r = "";
for (var i = this.length - 1; i >= 0; i--) {
r += this[i];
}
return r;
};
var s = "SimonTimothy";
console.log(s.getReversed());