Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
125 lines (112 loc) · 3.83 KB
/
Copy pathscript.js
File metadata and controls
125 lines (112 loc) · 3.83 KB
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
120
121
122
123
124
125
// These will hold arrays of strings that will be filterd from objects that will be pulled from parsed CSV file
let femTitles = [];
let maleTitles = [];
let nbTitles = [];
// Generates a random number between 0 and the array length -1 (cos Math.floor rounds down)
function randomIndex(array) {
return Math.floor(Math.random() * array.length)
};
// Kinda don't use this anymore, but it's here just in case
function selectRandom(array) {
let word = "";
word = array[randomIndex(array)];
return word;
};
/*
* This will hold the function callback for printing to the dom - this needs extra planning!
* It needs to work with the sorting algorithm to select the right sort for user selections
* and then do all the randomizations accordingly, and print the correct combination on the screen.
* To make the app re-usable, I may need to clear out previously sorted arrays?
* Perhaps that's a final 'then' statement that clears the arrays once the whole promise chain is finished.
*/
var printToDom = function (array, data) {
selectThenPrint(array);
return data;
};
// Gets the div element with the ID 'output' and inserts a word pulled from the given array at random.
function selectThenPrint(array) {
document.getElementById("output").innerHTML = array[randomIndex(array)];
};
var gotDataCallback = function (data) {
console.log('This is the data I successfully got:');
console.table(data);
console.log(data);
return data
};
var sortDataCallback = function (data) {
data.forEach(element => {
if (element.category === "title") {
if (element.gender === "F") {
femTitles.push(element.word);
} else if (element.gender === "M") {
maleTitles.push(element.word);
} else if (element.gender === "NB") {
nbTitles.push(element.word);
};
};
});
return data;
};
var sortedArrayCheckCallback = function (data) {
console.log("The arrays are:");
console.log("femTitles: " + femTitles);
console.log("maleTitles: " + maleTitles);
console.log("nbTitles: " + nbTitles);
return data
};
// *Use D3 to fetch my CSV file and turn it into an array of objects
// Columns: category (title, name), word (unique), gender(M, F, NB)
var data = d3.csv("name_gen_words.csv", function (data) {
return {
category: data.category,
word: data.word,
gender: data.gender
};
})
.then(gotDataCallback)
.then(sortDataCallback)
.then(sortedArrayCheckCallback)
.then(function () {
selectThenPrint(nbTitles)
});
/*
var data = d3.csv("name_gen_words.csv", function (data) {
return {
category: data.category,
word: data.word,
gender: data.gender
};
})
.then(function (data) {
console.log(`why won't this work: ${data}`);
console.table(data);
console.log(data);
return data
})
.then(function (data) {
data.forEach(element => {
if (element.category === "title") {
if (element.gender === "F") {
femTitles.push(element.word);
} else if (element.gender === "M") {
maleTitles.push(element.word);
} else if (element.gender === "NB") {
nbTitles.push(element.word);
};
};
});
})
.then(function () {
console.log(femTitles)
})
.then(function () {
selectThenPrint(nbTitles)
});
*/
/* var data = d3.csv("/projects/tests_and_experiments/PetNameGenerator/name_gen_words.csv",function(data){
let nameDataArray = [{
category: data.category,
word: data.word,
gender: data.gender
}]
}); */