forked from boisvert42/mechapuzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.txt
74 lines (58 loc) · 1.96 KB
/
todo.txt
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
// This function gets the input of words in a <textarea id="data">
// and puts them into an array, splitting by newline
$(function () {
var lines = [];
$.each($('#data').val().split(/\n/), function (i, line) {
if (line) {
lines.push(line);
}
});
console.log(lines);
});
// Here is one possible way to write a JavaScript function that takes the first letter of each word in a sentence to form a new word:
function acronym(sentence) {
// Split the sentence into an array of words
const words = sentence.split(" ");
// Create an empty array to store the acronym
const acronym = [];
// Loop through the words and get the first letter of each word
for (const word of words) {
acronym.push(word[0]);
}
// Join the letters in the acronym array and return the result
return acronym.join("");
}
// Example usage
const result = acronym("Significant Eastern City of North Dakota");
console.log(result); // Output: SECOND
// Find similar pairs from a collection of words
function singleLetterPairs(words) {
// Create an empty array to store the pairs
const pairs = [];
// Loop through the words and compare each word to every other word
for (let i = 0; i < words.length; i++) {
for (let j = 0; j < words.length; j++) {
// Check if the words are not the same word and have only one letter different
if (i !== j && diff(words[i], words[j]) === 1) {
// Add the pair to the array
pairs.push([words[i], words[j]]);
}
}
}
// Return the array of pairs
return pairs;
}
// Helper function to count the number of differences between two words
function diff(a, b) {
let diff = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
diff++;
}
}
return diff;
}
// Example usage
const words = ["tour", "force", "nom", "plume", "sour", "forte", "non", "plumb"];
const pairs = singleLetterPairs(words);
console.log(pairs); // Output: [["tour", "sour"], ["force", "forte"]]