-
Notifications
You must be signed in to change notification settings - Fork 0
/
sameFrequency.js
133 lines (106 loc) · 4.12 KB
/
sameFrequency.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//Can I restate the problem in my own words ? Write a function that accepts two integers and determine if the parameters have the same frequency of digits
//What are the inputs that go into the problem ? Two integers, positive. Will/can they be a string? How large are the integers, are they decimal or floating points? Will we always be dealing with 2 inputs/what if there's more? Only 2 for this challenge
//What are the outputs that should come from the solution to the problem ? return true/false. object containing the frequency of digits for either one or both of the integers
//Can the outputs be determined from the inputs ? Do I have enough information to solve the problem ?
//May not be able to answer this until I start solving the problem
//How should I label the important pieces of data that are part of the problem ? string or number? num1, num2 as the parameters, frequencyDigits as the object that holds the digit frequencies
//this solution does not work
function sameFrequency(num1, num2) {
//check the length of the two integers
if(num1.length !== num2.length) {
return false;
}
//create empty obj for num1 digit count
let num1LookUp = {};
//loop through num1 and add its key/value to the object
for( let key of num1 ) {
num1LookUp[key] = (num1LookUp[key] || 0) + 1; //if it doesn't already exist initialize it to 0 then add 1
}
//loop through the num2
for (let value of num2 ) {
console.log(num1LookUp);
//check if the key in num2 exists in the obj
if(!num1LookUp[value]) {
//if it doesn't return false
console.log("value not in obj: " + value)
return false;
}
//if it does, decrement the count by 1 in the object
else {
num1LookUp[value]--;
}
// if(num1LookUp[value]) {
// num1LookUp[value]--;
// } else {
// return false;
// }
}
console.log(num1LookUp);
console.log("true");
return true;
}
//define function
//This solution works, and so does the commented out code below + 81-88 commented out
function sameFrequencyUpdated(input1, input2) {
//convert integers to a string
let str1 = input1.toString()
let str2 = input2.toString()
//first check that the lengths of each string are equal
if ( str1.length !== str2.length ) {
return false;
}
//create an empty obj
let str1Count = {}
// let str2Count = {}
//store the values for each input in respective objects
for ( let key of str1 ) {
str1Count[key] = ( str1Count[key] || 0 ) + 1
}
// for ( let key of str2 ) {
// str2Count[key] = (str2Count[key] || 0) + 1
// }
//compare the two objects
// for ( let value in str1Count ) {
// //Do they have the same value frequency?
// if( str1Count[value] !== str2Count[value] ) {
// //If no, return false
// console.log("false");
// return false;
// }
// }
for ( let key of str2 ) {
if( !str1Count[key] ) {
console.log("false");
return false;
} else {
str1Count[key]--;
}
}
//If yes, return true
console.log("true");
return true;
}
sameFrequencyUpdated(88228, 28288);
//function accepts two integers and determines if they have the same frequency of values
//inputs: positive integers
//output: return either true or false
//I do have enough info to solve the problem
//either one or two empty objects to store the values of the two integers for comparing
function frequencyCounter(num1, num2) {
if (num1.length !== num2.length) return false;
let num1Lookup = {};
//loop through num1 and add each value to num1Lookup
for (let key of num1) {
num1Lookup = (num1Lookup[key] || 0) + 1;
}
//loop through second num1 and compare the frequency of the values to those in num1Lookup
for (let val of num2) {
if (!num1Lookup[val]) {
return false;
} else {
num1Lookup[val]--;
}
}
return true;
}
frequencyCounter(18383, 29392);