A general chat for developers. Don't ask to ask, just ask. Here's how: https://stackoverflow.com/help/how-to-ask
//using base unit data this function returns the equivalent data for other units
function response(data) {
let obj = {};
obj.bits = data;
obj.kbits = data / 1000;
obj.mbits = data / (1000 * 1000);
obj.gbits = data / (1000 * 1000 * 1000);
obj.tbits = data / (1000 * 1000 * 1000 * 1000);
obj.bytes = data / 8;
obj.kilobytes = data / 8000;
obj.megabytes = data / (8000 * 1000);
obj.gigabytes = data / (8000000 * 1000);
obj.terabytes = data / (8000000 * 1000 * 1000);
return obj;
}
// convert any units data into base unit data ( here bits)
function toBits(data, unit) {
let obj = {};
obj.b = data;
obj.kb = data * 1000;
obj.mb = data * 1000 * 1000;
obj.gb = data * 1000 * 1000 * 1000
obj.tb = data * 1000 * 1000 * 1000 * 1000;
obj.B = data * 8;
obj.KB = data * 8000;
obj.MB = data * 8000 * 1000;
obj.GB = data * 8000000 * 1000;
obj.TB = data * 8000000 * 1000 * 1000;
console.log(obj);
for (let key in obj) {
if (key == unit) {
return obj[key];
}
}
}
Basically I'm considering bit as base unit, so whatever unit user selects it will be converted to its bits form and fed into response function to get other equivalent unit's data.
@Srinath-V-S Before I answer your question, let me give you a few tips. Firstly, in the toBits
function, you're constructing a large object and then throwing away all of it except for one of its properties. You could avoid this, performing only the calculation you need, by using a switch
statement like this:
function toBits(data, unit) {
switch(unit) {
case 'b': return data;
case 'kb': return data * 1000;
case 'mb': return data * 1000 * 1000;
case 'gb': return data * 1000 * 1000 * 1000
case 'tb': return data * 1000 * 1000 * 1000 * 1000;
case 'B': return data * 8;
case 'KB': return data * 8000;
case 'MB': return data * 8000 * 1000;
case 'GB': return data * 8000000 * 1000;
case 'TB': return data * 8000000 * 1000 * 1000;
}
}
However, you can still do better based on the next two tips.
Secondly, I recommend using consistent names for the units. In response
you're writing the names in full, while in toBits
, you're using abbreviations. On top of that, those abbreviations aren't consistent with the SI prefixes. It's "kB", not "KB", and "Mb"/"Gb"/"Tb", not "mb"/"gb"/"tb". Using consistent names makes it easier for yourself to predict which name you should be using in your code, and also helps with the next tip I'm about to give.
Thirdly, note that you're repeating the numbers in both functions. You can share them in a common variable so you don't have to repeat yourself:
const unitFactors = {
b: 1,
kb: 1000,
Mb: 1000 * 1000,
Gb: 1000 * 1000 * 1000,
Tb: 1000 * 1000 * 1000 * 1000,
B: 8,
kB: 8 * 1000,
MB: 8 * 1000 * 1000,
GB: 8 * 1000 * 1000 * 1000,
TB: 8 * 1000 * 1000 * 1000 * 1000,
};
function response(bits) {
const obj = {};
for (let unit in unitFactors) {
obj[unit] = bits / unitFactors[unit];
}
return obj;
}
function toBits(value, unit) {
return value * unitFactors[unit];
}
This has the added benefit that if you want to show a dropdown with units, you can just use Object.keys(unitFactors)
(or _.keys(unitFactors)
if using Underscore or Lodash) to get an array with all the unit names. Also, if you want to display full unit names somewhere, you can just use a second mapping from abbreviations to full names:
const unitFullNames = {
b = 'bit',
kb = 'kilobit',
Mb = 'megabit',
Gb = 'gigabit',
Tb = 'terabit',
B = 'byte',
kB = 'kilobyte',
MB = 'megabyte',
GB = 'gigabyte',
TB = 'terabyte',
};
Now to answer your original question. You may have already realised that if the user inputs a value
and a unit
, you can obtain an object with equivalent values in all units (including the one the user entered) with the expression response(toBits(value, unit))
. If I understood your question correctly, you want to remove the unit that the user entered from this object. A simple way to do that is with the delete
operator:
const otherUnits = response(toBits(value, unit));
delete otherUnits[unit];
Underscore has a helper function omit
that lets you achieve the same thing in a single expression:
const otherUnits = _.omit(response(toBits(value, unit)), unit);
I hope this helps!
Hey I want to build an chatting app which includes 4 memers chatting and with video calling and audio calling + chatting what I do?
I'd first try to build a text only chat between 2 people, after I knew how to do that I'd try and implement on top off it. Don't start from the end start from the start
CodeMirror.MergeView
, I'm getting Uncaught TypeError: CodeMirror.MergeView is not a function
this error. Please help me what i'm doing wrong?.@jgonggrijp
var value, orig1, orig2, dv, panes = 2,
highlight = true,
connect = null,
collapse = false;
initUI();
function initUI() {
var target = document.getElementById("view");
target.innerHTML = "";
let dv = CodeMirror.MergeView(target, {
value: value,
origLeft: panes == 3 ? orig1 : null,
orig: orig2,
lineNumbers: true,
mode: "",
highlightDifferences: highlight,
connect: connect,
collapseIdentical: collapse
});
}
I thought once its constructor is initialized i'll get two panes in the UI but getting that error mentioned above.
grid grid-cols-7
. I have been using multiple methods(float and flex) to properly render and align the divs, but can't seem to make them work the way it should be. Any insight into what is happening here will be helpful.
Hi,
I decided I want to try to get my first Software Development job.
How is my portfolio, so far?
https://ronald-ronaldmcorona.vercel.app
Hi. This might be a silly thing to ask, but I need help in understanding how 'sed' commands work. Like what is the difference between
sed -i -e "s/# tlsProvider=.*/tlsProvider=OpenSSL/" apache-pulsar-2.7.2/conf/bookkeeper.conf
and
sed -i -e "s/tlsProvider=.*/tlsProvider=OpenSSL/" apache-pulsar-2.7.2/conf/bookkeeper.conf
Whats with the 's/#' and the 's/' ?