-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops.js
70 lines (63 loc) · 2.33 KB
/
loops.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
/* eslint-disable linebreak-style */
/* eslint-disable no-unused-vars */
const repeat = (inString, inNum) => {
let outputString = '';
for (let i = 0; i < inNum; i++) outputString += inString;
return outputString;
}
const sum = (inNumArray) => {
let outputNum = 0;
for (let i = 0; i < inNumArray.length; i++) outputNum += inNumArray[i];
return outputNum;
}
const join = (inArray, seperator = '') => {
let outputString = '';
for (let i = 0; i < inArray.length; i++) (i === 0) ? outputString += inArray[0] : outputString += seperator + inArray[i];
return outputString;
}
const gridGenerator = (gridSize) => {
let theGrid = '';
for (let row = 1; row <= gridSize; row++) {
for (let col = 1; col <= gridSize; col++) {
((row % 2 !== 0 && col % 2 !== 0) || (row % 2 === 0 && col % 2 === 0)) ?
theGrid += '#' :
theGrid += ' ';
}
theGrid += `\n`;
}
return theGrid;
}
const paramify = (inObject) => {
let outArray = [];
for (let i in inObject) {
if (inObject.hasOwnProperty(i)) outArray.push(i + '=' + inObject[i]);
}
return outArray.sort().join('&');
}
const paramifyObjectKeys = (inObject) => {
let outArray = [];
let keys = Object.keys(inObject);
for (let i = 0; i < keys.length; i++) outArray.push(keys[i] + '=' + inObject[keys[i]]);
return outArray.sort().join('&');
}
//console.log(paramifyObjectKeys({ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }));
const sort = (inputArray) => {
let outputArray = [];
let itemCount = inputArray.length;
while (outputArray.length < itemCount) {
// set the lowest to compare with the first remaining inputItem
let currentItem = {lowest: inputArray[0], index: 0};
// find the lowest item remaining in the inputand splice it out of the input
for (let i = 1; i < inputArray.length; i++) {
// keep finding the lowest item left in input
if (inputArray[i] < currentItem.lowest) {
currentItem.lowest = inputArray[i];
currentItem.index = i;
}
}
// push the lowest to the outputArray
inputArray.splice(currentItem.index, 1);
outputArray.push(currentItem.lowest);
}
return outputArray;
}