Riza Khan
Riza Khan

Reputation: 3158

ALGORITHM - File Naming

You are given an array of desired filenames in the order of their creation. Since two files cannot have equal names, the one which comes later will have an addition to its name in a form of (k), where k is the smallest positive integer such that the obtained name is not used yet.

Return an array of names that will be given to the files.

Example

For names = ["doc", "doc", "image", "doc(1)", "doc"], the output should be fileNaming(names) = ["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"].

One person posted this solution:

const fileNaming = names => {
    const used = {};
    return names.map(name => {
        let newName = name;
        while (used[newName]) {
            newName = `${name}(${used[name]++})`;
        }
        used[newName] = 1;
        return newName;
    });
};

I'm having a hard time understanding the while block's condition.

used is an empty object.

newName is a new variable that is equal to the current item in the names array.

How does used[newName] resolve to a number? used is never set to anything other then an empty object.

This is the console output for console.log(used[newName])

enter image description here

Using this input:

["dd", 
 "dd(1)", 
 "dd(2)", 
 "dd", 
 "dd(1)", 
 "dd(1)(2)", 
 "dd(1)(1)", 
 "dd", 
 "dd(1)"]

Upvotes: 2

Views: 427

Answers (1)

tucuxi
tucuxi

Reputation: 17955

In JavaScript, {} is an empty object - and as such, it can contain any number of key-value pairs. A key that is not defined has the value undefined, which evaluates to false when tested. However, any non-zero, non-NaN numerical value will evaluate to true:

console.log({}["a key that does not exist"])                 // undefined
while (undefined) {
    console.log("this should never be printed");             // never executes
}
while ({}["a key that does not exist"]) {
    console.log("this should never be printed");             // never executes
}
if (1) {
    console.log("positive numbers are true");                // writes to console
}

You can use objects as maps (even though you can also use actual maps instead in newer code)

A simpler version of the above program would have written

return names.map(name => {
    let newName = name;
    while (used[newName] !== undefined) {       // tests for "not undefined"
        newName = `${name}(${used[name]})`;     // uses current value
        used[name] = used[name] + 1;            // sets a larger value            
    }
    used[newName] = 1;                          // sets a value
    return newName;
});

Upvotes: 1

Related Questions