I'm creating this Hash Table in JavaScript and I want it warn me each time I'm adding existing values in the array. I tried my own implementation, but it isn't working.
function hashFunction(s,tableSize){
let hash = 17;
for(let i = 0; i<s.length; i++){
hash = (13*hash * s.charCodeAt(i)) % tableSize;
}
return hash;
};
class HashTable {
table = new Array(2000);
numItems = 0;
loadFactor = this.numItems/this.table.length;
setItem = (key,value)=>{
const idx = hashFunction(key, this.table.length);
if(this.table.includes(key)){
return "already exists"
}else this.numItems++
if(this.table[idx]){
this.table[idx].push([key,value])
}else this.table[idx]=[[key,value]];
};
getItem = (key)=>{
const idx = hashFunction(key,this.table.length);
if(!this.table[idx]){
return "Key doesn't exist";
}else
return this.table[idx].find(x=>x[0]===key)[1];
};
};
let myHash = new HashTable
myHash.setItem("first","daniel")
myHash.setItem("last","esposito")
myHash.setItem("age","21")
myHash.setItem("height","1,90")
It would be beneficial to review implementing pseudo code and existing implementations. There were numerous errors which can summarized as the original implementation failing to correctly handle pairs within a bucket.
Here is a working update which salvages some of the structure while discarding most of the inconsistent and/or incorrect implementation - I've left comments as reference points to understand why the original was incorrect. The code has been structured to illustrate access/creation of buckets, access/creation of pairs within the buckets, and behavior selection depending on the case.
YMMV.
function hashFunction(s, tableSize){
let hash = 17;
for (let i = 0; i < s.length; i++){
hash = (13 * hash + s.charCodeAt(i)) % tableSize;
// ^-- Ry suggests the original * is a typo.
}
return hash;
};
class HashTable {
// I've eliminated 'load factor' to cover a basic implementation.
// See rebalancing, good table size selection, alternative collision
// resolution strategies, etc.
// This implementation might pass a CS101 course.
// Yes, for THIS EXAMPLE the TABLE SIZE IS CHOSEN AS 2.
// This ensures that there are multiple items per bucket
// which guarantees the collision resolution paths are invoked.
// This is a terrible choice in practice.
table = new Array(2);
numItems = 0;
setItem = (key, value)=>{
const idx = hashFunction(key, this.table.length);
// set/get should ONLY access simple properties or
// a BUCKET from the hash code as top-level structures.
// (Using table.includes(..) here is fundamentally INCORRECT.)
let bucket = this.table[idx];
if (bucket) {
// Existing bucket. Search in HERE to see if the key exists.
// This should generally be the same code as the "get".
let pair = bucket.find(x => x[0] === key);
if (pair) {
// Same pair, update value.
pair[1] = value;
return false; // "existing"
} else {
// Add new pair to bucket.
bucket.push([key, value]);
this.numItems += 1;
return true; // "new"
}
} else {
// Create a new bucket and item pair.
this.table[idx] = [[key, value]];
this.numItems += 1;
return true; // "new"
}
};
getItem = (key) =>{
const idx = hashFunction(key, this.table.length);
// Code should match close to 'set'
let bucket = this.table[idx];
if (bucket) {
let pair = bucket.find(x => x[0] === key);
if (pair) {
// Bucket exists and key exists within bucket.
return pair[1];
}
}
// The result should be the same if the key doesn't exist because
// bucket is not found, or if the bucket is found and the
// key does not exist within the bucket..
return undefined;
};
}
let myHash = new HashTable
var items = [
["first", "daniel"],
["last", "esposito"],
["age", 21],
["height", 1.9]
]
// Insert multiple values:
// Check to see inserted report true/not,
// and that the numItems is increased appropriately.
for (let run of [1, 2]) {
for (let i of items) {
let [k, v] = i;
var inserted = myHash.setItem(k, v);
var found = myHash.getItem(k) === v;
console.log(`run=${run} key=${k} value=${v} inserted=${inserted} numItems=${myHash.numItems} found=${found}` )
}
}
Output:
run=1 key=first value=daniel inserted=true numItems=1 found=true
run=1 key=last value=esposito inserted=true numItems=2 found=true
run=1 key=age value=21 inserted=true numItems=3 found=true
run=1 key=height value=1.9 inserted=true numItems=4 found=true
run=2 key=first value=daniel inserted=false numItems=4 found=true
run=2 key=last value=esposito inserted=false numItems=4 found=true
run=2 key=age value=21 inserted=false numItems=4 found=true
run=2 key=height value=1.9 inserted=false numItems=4 found=true
hash += (13*hash * s.charCodeAt(i)) % tableSize;
Related
I'm trying to apply BFS to find the length of the shortest path in a graph, but am not quite getting the right result.
I try to find the shortest path by visiting each node in the graph; then mark the ones that are visited, and continue recording the length of the path. What I hope to return is an array that contains the shortest path, but I think I am doing something wrong in the process.
I think this has something to do with how I am indexing my arrays and recording my distances.
My input is currently formatted in the form of an array that contains the neighbors for each vertex i. So, for instance, graph[i] would give you an array of neighbors of vertex i.
Any thoughts on how I can go about fixing my issue would be very helpful. Thanks!
var shortestPathLength = function(graph) {
let distances = []
let pq = []
distances[0] = 0
let mygraph = {}
for (var i = 0; i<graph.length; i++) {
distances[i] = -1
mygraph[i] = graph[i]
}
pq.push(mygraph[0])
while(pq.length > 0) {
let min_node = pq.shift()
for(var i = 0; i<min_node.length; i++) {
candidate = distances[i] + 1
if(distances[min_node[i]]== -1) {
distances[min_node[i]] = distances[i] + 1
pq.push(graph[min_node[i]])
}
else if (candidate < distances[min_node[i]]) {
distances[min_node[i]] = distances[i] + 1
}
}
}
function getSum(total, num) {
return total + num;
}
console.log(distances)
return distances.length
};
Your problem is candidate = distances[i] + 1. The i is the index of the edge inside the min_node, which isn't interesting at all. What you are looking for is the current distance to the min_node. You will need to either assign the distance as a property of the min_node object itself, or you will need to store the id (index in graph) of the nodes in your queue instead of the object itself.
I've made a few other simplifications, but the only showstopper problem in your code was the distance lookup.
function shortestPathLength = function(graph) {
const distances = Array(graph.length).fill(-1);
distances[0] = 0; // start node
const queue = [0];
while (queue.length > 0) {
const node_index = queue.shift();
// ^^^^^
const edges = graph[node_index]; // get the node itself
const candidate = distances[node_index] + 1; // outside of the loop
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
for (const target in edges) {
if (distances[target] == -1) {
distances[target] = candidate;
queue.push(target); // not graph[target]
// ^^^^^^
}
}
}
return distances;
}
So i have this array
[ 'vendor/angular/angular.min.js',
'vendor/angular-nice-bar/dist/js/angular-nice-bar.min.js',
'vendor/angular-material/modules/js/core/core.min.js',
'vendor/angular-material/modules/js/backdrop/backdrop.min.js',
'vendor/angular-material/modules/js/dialog/dialog.min.js',
'vendor/angular-material/modules/js/button/button.min.js',
'vendor/angular-material/modules/js/icon/icon.min.js',
'vendor/angular-material/modules/js/tabs/tabs.min.js',
'vendor/angular-material/modules/js/content/content.min.js',
'vendor/angular-material/modules/js/toolbar/toolbar.min.js',
'vendor/angular-material/modules/js/input/input.min.js',
'vendor/angular-material/modules/js/divider/divider.min.js',
'vendor/angular-material/modules/js/menu/menu.min.js',
'vendor/angular-material/modules/js/select/select.min.js',
'vendor/angular-material/modules/js/radioButton/radioButton.min.js',
'vendor/angular-material/modules/js/checkbox/checkbox.min.js',
'vendor/angular-material/modules/js/switch/switch.min.js',
'vendor/angular-material/modules/js/tooltip/tooltip.min.js',
'vendor/angular-material/modules/js/toast/toast.min.js',
'vendor/angular-clipboard/angular-clipboard.js',
'vendor/angular-animate/angular-animate.min.js',
'vendor/angular-aria/angular-aria.min.js',
'vendor/angular-messages/angular-messages.min.js',
'vendor/angular-ui-router/release/angular-ui-router.js',
'src/app/about/about.js',
'src/app/hekate.cfg.js',
'src/app/hekate.ctrl.js',
'src/app/hekate.module.js',
'src/app/home/home.js',
'src/app/user/dialog/user.signIn.ctrl.js',
'src/app/user/dialog/user.signIn.module.js',
'src/app/user/user.cfg.js',
'src/app/user/user.ctrl.js',
'src/app/user/user.module.js',
'src/common/services/toast.service.js',
'templates-common.js',
'templates-app.js'
]
And taking the following part from the above array as example:
[
'src/app/hekate.cfg.js',
'src/app/hekate.ctrl.js',
'src/app/hekate.module.js',
]
I want to sort it like
[
'src/app/hekate.module.js',
'src/app/hekate.cfg.js',
'src/app/hekate.ctrl.js',
]
So more specific of what i want is to find in that array where string is duplicated and after check if has at the end [.cfg.js, .ctrl.js, .module.js] and automatic order them to [.module.js, .cfg.js, .ctrl.js]
Can anyone please help me with that?
A single sort proposal.
var array = ['src/app/about/about.js', 'src/app/hekate.cfg.js', 'src/app/hekate.ctrl.js', 'src/app/hekate.module.js', 'src/app/home/home.js', 'src/app/user/dialog/user.signIn.ctrl.js', 'src/app/user/dialog/user.signIn.module.js', 'src/app/user/user.cfg.js', 'src/app/user/user.ctrl.js', 'src/app/user/user.module.js'];
array.sort(function (a, b) {
function replaceCB(r, a, i) { return r.replace(a, i); }
var replace = ['.module.js', '.cfg.js', '.ctrl.js'];
return replace.reduce(replaceCB, a).localeCompare(replace.reduce(replaceCB, b));
});
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');
To prevent so much replaces, i suggest to have a look to sorting with map.
You can try something like this:
Algo:
Group based on path and store file names as value.
Check for existence of one of special file ".cfg.js"
Sort following list based on custom sort.
Loop over object's property and join key with values to form full path again.
If you wish to sort full array, you can sort keys itself and then merge path with names. I have done this. If you do not wish to do this, just remove sort function from final loop.
Sample
var data=["vendor/angular/angular.min.js","vendor/angular-nice-bar/dist/js/angular-nice-bar.min.js","vendor/angular-material/modules/js/core/core.min.js","vendor/angular-material/modules/js/backdrop/backdrop.min.js","vendor/angular-material/modules/js/dialog/dialog.min.js","vendor/angular-material/modules/js/button/button.min.js","vendor/angular-material/modules/js/icon/icon.min.js","vendor/angular-material/modules/js/tabs/tabs.min.js","vendor/angular-material/modules/js/content/content.min.js","vendor/angular-material/modules/js/toolbar/toolbar.min.js","vendor/angular-material/modules/js/input/input.min.js","vendor/angular-material/modules/js/divider/divider.min.js","vendor/angular-material/modules/js/menu/menu.min.js","vendor/angular-material/modules/js/select/select.min.js","vendor/angular-material/modules/js/radioButton/radioButton.min.js","vendor/angular-material/modules/js/checkbox/checkbox.min.js","vendor/angular-material/modules/js/switch/switch.min.js","vendor/angular-material/modules/js/tooltip/tooltip.min.js","vendor/angular-material/modules/js/toast/toast.min.js","vendor/angular-clipboard/angular-clipboard.js","vendor/angular-animate/angular-animate.min.js","vendor/angular-aria/angular-aria.min.js","vendor/angular-messages/angular-messages.min.js","vendor/angular-ui-router/release/angular-ui-router.js","src/app/about/about.js","src/app/hekate.cfg.js","src/app/hekate.ctrl.js","src/app/hekate.module.js","src/app/home/home.js","src/app/user/dialog/user.signIn.ctrl.js","src/app/user/dialog/user.signIn.module.js","src/app/user/user.cfg.js","src/app/user/user.ctrl.js","src/app/user/user.module.js","src/common/services/toast.service.js","templates-common.js","templates-app.js"];
// Create groups based on path
var o = {};
data.forEach(function(item) {
var lastIndex = item.lastIndexOf('/') + 1;
var path = item.substring(0, lastIndex);
var fname = item.substring(lastIndex);
if (!o[path]) o[path] = [];
o[path].push(fname);
});
var manualOrder= [".module.js", ".cfg.js", ".ctrl.js"];
Array.prototype.fuzzyMatch = function(search){
return this.some(function(item){
return item.indexOf(search)>-1;
});
}
Array.prototype.fuzzySearchIndex = function(search){
var pos = -1;
this.forEach(function(item, index){
if(search.indexOf(item)>-1){
pos = index;
}
});
return pos;
}
function myCustomSort(a,b){
var a_pos = manualOrder.fuzzySearchIndex(a);
var b_pos = manualOrder.fuzzySearchIndex(b);
return a_pos > b_pos ? 1 : a_pos < b_pos ? -1 : 0;
}
// Check for ".cfg.js" and apply custom sort
for (var k in o) {
if (o[k].fuzzyMatch(".cfg.js")) {
o[k].sort(myCustomSort);
}
}
// Merge Path and names to create final value
var final = [];
Object.keys(o).sort().forEach(function(item) {
if (Array.isArray(o[item])) {
final = final.concat(o[item].map(function(fn) {
return item + fn
}));
} else
final = final.concat(o[item]);
});
console.log(final);
First make an array for names like 'hekate'.
Then make an array for final results.
We need 3 searching loops for ctrls, cfgs and modules.
If string contains arrayWithNames[0] + '.module' push the whole record to new array that you created. Same with ctrls and cfgs.
var allItems = []; //your array with all elements
var namesArray = [];
var finalResultsArray = [];
//fill name array here:
for(var i=0; i<=allItems.length; i++){
//you have to split string and find the module name (like 'hekate'). i hope you know how to split strings
}
//sort by modules, cfgs, ctrls:
for(var i=0; i<=namesArray.length; i++){
if(allItems[i].indexOf(namesArray[i] + '.module') > -1) {
finalResultsArray.push(allItems[i]);
}
}
for(var i=0; i<=namesArray.length; i++){
if(allItems[i].indexOf(namesArray[i] + '.cfg') > -1) {
finalResultsArray.push(allItems[i]);
}
}
for(var i=0; i<=namesArray.length; i++){
if(allItems[i].indexOf(namesArray[i] + '.ctrl') > -1) {
finalResultsArray.push(allItems[i]);
}
}
//now finalResultsArray have what you wanted
You can provide your own compare function to array.sort (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
Write one that returns the correct order for modules, ctrls and cfgs:
It should first remove the suffixes, and if the rest is the same, use the correct logic to return the order according to the suffix. Otherwise return a value according to the alphabetical order.
Update
I didn't test this code (not is it finished), but it should look something like that:
arr.sort(function(a, b) {
if ((a.endsWith(".cfg.js") || a.endsWith(".ctrl.js") || a.endsWith(".module.js")) &&
(b.endsWith(".cfg.js") || b.endsWith(".ctrl.js") || b.endsWith(".module.js"))) {
var sortedSuffixes = {
".module.js": 0,
".cfg.js": 1,
".ctrl.js": 2
};
var suffixAIdx = a.lastIndexOf(".cfg.js");
if (suffixAIdx < 0) suffixAIdx = a.lastIndexOf(".ctrl.js");
if (suffixAIdx < 0) suffixAIdx = a.lastIndexOf(".module.js");
var suffixBIdx = b.lastIndexOf(".cfg.js");
if (suffixBIdx < 0) suffixBIdx = b.lastIndexOf(".ctrl.js");
if (suffixBIdx < 0) suffixBIdx = b.lastIndexOf(".module.js");
var prefixA = a.substring(0, suffixAIdx);
var prefixB = b.substring(0, suffixAIdx);
if (prefixA != prefixB)
{
return a.localeCompare(b);
}
var suffixA = a.substring(suffixAIdx);
var suffixB = b.substring(suffixBIdx);
return sortedSuffixes[suffixA] - sortedSuffixes[suffixB];
} else {
return a.localeCompare(b);
}
});
Update 2
Here is a fiddle (https://jsfiddle.net/d4fmc7ue/) that works.
I have a JavaScript array that contains some words that cannot be used when requesting user accounts to be created.
I am trying to loop over the accounts requested and check them against a word filter. If they contain any of the words, the value is moved to an array of "Invalid Accounts".
// Create our arrays
var blacklist = ["admin", "webform", "spoof"];
var newAccounts = ["admin1#google.com", "interweb#google.com", "puppy#google.com"];
var invalidAccounts = [];
// I need to check if any of the new accounts have matches to those in the blacklist.
// admin1#google.com would need to be pushed into the invalidAccounts array because it
// contains the word admin. Although interweb contains the word web, it does not contain
// the full word, webform, so should be ignored.
// Loop over the blacklist array
for(var x = 0; x < blacklist.length; x++){
if(blacklist[x].indexOf(newAccounts) > -1){
alert(blacklist[x] + " is in the blacklist array");
// Push the newAccounts value into the invalidAccounts array since it contains
// a blacklist word.
} else {
alert('no matches');
}
}
What do I need to change in the above code to have it match the partial strings such as mentioned?
Fiddle of above code: https://jsfiddle.net/1qwze81o/
You probably won't need to use all of this but it should be helpful none the less:
var blacklist = ["admin", "webform", "spoof"];
var newAccounts = ["admin1#google.com", "interweb#google.com", "puppy#google.com"];
var invalidAccounts = [];
// transform each email address to an object of the form:
// { email: string, valid: boolean }
var accountObjects = newAccounts.map(function (a) {
return { email: a, valid: true };
});
// loop over each account
accountObjects.forEach(function (account) {
// loop over the blacklisted terms
blacklist.forEach(function (blacklisted) {
// check to see if your account email address contains a black listed term
// and set the valid property accordingly
account.valid = account.email.search(blacklisted) === -1;
});
});
// filter accountObjects, validAccounts will now contain an array of valid objects
var validAccounts = accountObjects.filter(function (a) {
return a.valid;
});
// back to the original type of a string array
var validEmailAddresses = validAccounts.map(function (a) {
return a.email;
});
A solution using javascript array native functions:
var invalidAccounts = newAccounts.filter(function(account){ // we need to filter accounts
return blacklist.some(function(word){ // and return those that have any of the words in the text
return account.indexOf(word) != -1
})
});
More info on: Array.filter and
Array.some
We need two loops to achieve this:
something like below:
// Loop over the blacklist array
for(var j = 0; x < newAccounts.length; j++){
for(var x = 0; x < blacklist.length; x++){
if(newAccounts[j].indexOf(blacklist[x]) > -1){
alert(blacklist[x] + " is in the blacklist array");
// Push the newAccounts value into the invalidAccounts array since it contains a blacklist word.
}else{
alert('no matches');
}
}
}
I fixed some of the things...
for (var x = 0; x < newAccounts.length; x++) // loop through new accounts
{
// for every account check if there is any match in blacklist array
for (var y = 0; y < blacklist.length; y++)
{
// if there is match do something
if (newAccounts[x].indexOf(blacklist[y]) > -1)
{
alert(newAccounts[x] + " is in the blacklist array");
break;
}
}
}
Here is the fiddle: https://jsfiddle.net/swaprks/n1rkfuzh/
JAVASCRIPT:
// Create our arrays
var blacklist = ["admin", "webform", "spoof"];
var newAccounts = ["admin1#google.com", "interweb#google.com", "puppy#google.com"];
var invalidAccounts = [];
for(var x = 0; x < newAccounts.length; x++){
for(var y = 0; y < blacklist.length; y++){
if( newAccounts[x].indexOf(blacklist[y]) > -1){
alert(blacklist[x] + " is in the blacklist array");
// Push the newAccounts value into the invalidAccounts array since it contains
// a blacklist word.
invalidAccounts.push(newAccounts[x]);
}else{
alert('no matches');
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I need a way to get the name of the variable with the greatest value.
a = 55;
b = 13;
c = 45;
d = 5;
var top = '';
if((a>b)&&(a>c)&&(a>d)){ top = 'a'; }
if((b>a)&&(b>c)&&(b>d)){ top = 'b'; }
if((c>a)&&(c>b)&&(c>d)){ top = 'c'; }
if((d>a)&&(d>b)&&(d>c)){ top = 'd'; }
Is there a better or faster way to do this?
You can't get the variable name directly:
Very few languages support what you want to do because the variable
names only exist for your benefit in the source code. They do not
exist in the compiled executable code in that form anymore and
tracking them would be extremely expensive.
If you want to do this, there is something fundamentally wrong with your design as there is no reason that would preclude doing it the most idiomatic way possible which is to use an associative array, which in JavaScript means using an Object or an actual Map when available and where appropriate.
Object based approach:
Most compatible way if you do not have access to Map:
You can use an object and find the property name. The most concise way to do this is with the Array.reduce() function.
var obj = {a:55,b:13,c:45,d:5};
var maxPropertyName = Object.keys(obj).reduce(function(previous,key){
return obj[previous] > obj[key] ? previous : key;
})
console.log(maxPropertyName);
Output:
a
Map based approach:
For this case a Map seems more appropriate since this looks like a homogeneousCollection rather than a Type of something.
Map instances are only useful for collections, and
you should consider adapting your code where you have previously used
objects for such. Objects shall be used as records, with fields and
methods. If you're still not sure which one to use, ask yourself the
following questions:
Are keys usually unknown until run time, do you need to look them
up dynamically?
Do all values have the same type, and can be used interchangeably?
Do you need keys that aren't strings?
Are key-value pairs often added or removed? Do you have an
arbitrary (easily changing) amount of key-value pairs?
Is the collection iterated? Those all are signs that you want a Map
for a collection.
If in contrast you have a fixed amount of keys, operate on them
individually, and distinguish between their usage, then you want an
object.
Here is how to add a .reduce() method to Map:
Map.prototype.reduce = function(callback){
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.reduce called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
var t = Object(this), value;
if (t.size === 0) { value = undefined; }
else if (t.size === 1) { value = t.keys().next().value; }
else {
value = t.keys().next().value;
for (var kv of t) {
value = callback(value, kv[0]);
}
}
return value;
};
Same .reduce() code now works:
var m = new Map([["a",55],["b",13], ["c",45],["d",5]]);
var maxPropertyName = m.reduce(function(previous,key){
return m.get(previous) > m.get(key) ? previous : key;
})
console.log(maxPropertyName);
Output:
a
A simple way to do it, store everything in an object and loop over the keys:
// you can keep the input variables
var a = 55;
var b = 13;
var c = 45;
var d = 5;
var obj = {
a: a,
b: b,
c: c,
d: d
}
var max;
var varName;
Object.keys(obj).forEach(function(key) {
if (!max || max < obj[key]) {
max = obj[key];
varName = key;
}
});
snippet.log(max);
snippet.log(varName);
<script src="https://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
function high(obj) {
var highest = Number.NEGATIVE_INFINITY,
keyName = null;
for (var key in obj) {
if( obj[key] > highest ) {
highest = obj[key];
keyName = key;
}
}
return keyName;
}
In this way you can need not care about the variable name.Also need not repeat the variable name.
// init
var obj={};
var max=0,maxName="";
var x={};
for(var key in window){
x[key]=window[key];
}
// your code
a = 55;
b = 13;
c = 45;
d = 5;
// end of your code
// select the max num
for(var key in window){
if(typeof(window[key]) == "number" && typeof(x[key])!="number"){
if(window[key]>max){
max=window[key];
maxName=key;
}
}
}
// output the variable name
console.log(maxName);
And you can write a method to reuse:
function getTop(func){
// init
var obj={};
var max=0,maxName="";
var x={};
for(var key in window){
x[key]=window[key];
}
func();
// select the max num
for(var key in window){
if(typeof(window[key]) == "number" && typeof(x[key])!="number"){
if(window[key]>max){
max=window[key];
window[key]=undefined;
maxName=key;
}
}
}
// output the variable name
return maxName;
}
you can use the code to get the top varname:
var top=getTop(function(){
a=11;
b=22;
c=23;
d=14;
});
One way, if you cannot use object, then get max value and then check individual values to variable. Here you will not have very complex condition. Also its better to if...else if...else than multiple if
a = 55;
b = 13;
c = 45;
d = 5;
var max = Math.max(a,b,c,d);
var varName = "";
if(max === a){
varName = "a";
}
else if(max === b){
varName = "b";
}
else if(max === c){
varName = "c";
}
else{
varName = d;
}
alert(varName);
This is problem-solving q; nothing's gone wrong, I'm just stumped about how to advance. Basically I want my users to be able to
Point to an arbitrary key of an object with arbitrary depth via a string representation of the "path";
Confirm that each step of the "path" exists; and
Implement CRUD-like functionality
I can verify that each key is valid, but I'm just stumped on how to then utilize said path without ultimately using an eval() statement, but of course I needn't explain why I'm not going to let a call to eval() get anywhere near arbitrary user input. Here's as far as I can get:
const SEP = "/" //in reality this is set by the server,
MyObjInterface = function() {
this.params = {};
this.response = {};
// suppose some initialization business, then on to the question... ( >.>)
this.updateOb= function(path, value ) {
path = path.replace('\\' + DS,"$DIRECTORY_SEPARATOR").split(DS);
for (var i = 0; i < path.length; i++) {
path[i].replace("$DIRECTORY_SEPARATOR",DS);
}
if (typeof(path) != "object" || path.length < 3) { // 3 = minimum depth to reach a valid field in any rset scheme
throw "Could not create rset representation: path either incorrectly formatted or incomplete."
}
var invalidPath = false;
var searchContext = path[0] === "params" ? this.params : this.response;
for (var i = 1; i < path.length - 1; i++) {
if (invalidPath) { throw "No key found in rset at provided path: [" + path[i] + "]."; }
if (i === path.length - 1) {
searchContext[path[1]] = value;
return true;
}
if (path[i] in searchContext) {
searchContext = searchContext[path[i]];
} else {
invalidPath = true;
}
}
}
};
You break the path up in to components, then you recursively walk the tree.
My JS is weak, so I'll pseudo code.
path = "go.down.three";
listOfElements = breakUpPath(path); // Now listOfElement = ["go", "down", "three"];
myObj = setObjectValue(myObj, listOfElement, "I'm a value");
function setObjectValue(obj, listOfElements, value) {
var firstPart = pop(listOfElements); // get and remove top of listOfElements stack
if (length(listOfElements) == 0) { // last element of the path, set the value
obj[firstPart] = value;
return obj;
} else {
// check if a property exists at all
var firstValue = obj[firstPart];
if (firstValue == null) {
firstValue = new Object();
obj[firstPart] = firstValue;
}
obj[firstPart] = setObjectValue(firstValue, listOfElement, value);
}
}
So, my as I said, my JS is weak (really weak, I can't really even spell JavaScript). Needless to say, this is untested.
But something like that is what you're looking for. Just work your way down the elements of the path.