Method Implementation That Takes 3 Parameters And Returns A Binary Number
Solution 1:
According to your requirement Encrypt user with parameters age (up to 255-> 11111111), Number of vacations (up to 15-> 1111), On vacation or not (1 or 0)
constcreate = (age, noOfVacations, onVacation) => {
return (
age.toString(2).padStart(8, '0') +
noOfVacations.toString(2).padStart(4, '0') +
onVacation.toString(2).padStart(1, '0')
);
};
const ret = create(30, 13, 1);
console.log(ret);
By the way, you can refactor the above code to make it more reusable by making a separated binary to decimal with zero padding function.
constbinToDecWithZeroPad = (param, n) => param.toString(2).padStart(n, '0');
constcreate = (age, noOfVacations, onVacation) =>
binToDecWithZeroPad(age, 8) +
binToDecWithZeroPad(noOfVacations, 4) +
binToDecWithZeroPad(onVacation, 1);
const ret = create(30, 13, 1);
console.log(ret);
If parameter number is unknown you can use rest parameter. Rest parameter syntax allows us to represent an indefinite number of arguments as an array. So you can use any number of parameter.
constcreate = (...params) => {
let str = '';
params.forEach((x) => (str += x.toString(2)));
return str;
};
const ret = create(30, 13, 1);
console.log(ret);
Update: I have not to checked if the parameter is a non numeric, decimal or negative in my code because if you need to check it you can easily add it by using simple if condition and as for adding zero dynamically you cannot use more than one rest parameter because this is the limitation that only one rest parameter is allowed in the function declaration. Although, you can solve it by using one rest parameter(think about it if you have time). By the way, you can also use object, single or multiple array whatever you want as a parameter to make it dynamic.
Solution 2:
This should work:
functiondec2bin(dec){
return (dec >>> 0).toString(2);
}
functioncreate(age, vacationNumber, vacation) {
var a = dec2bin(age).padStart(8, "0"); // padEnd adds zeros to match sizevar b = dec2bin(vacationNumber).padStart(4, "0");
var c = vacation==true||vacation==1 ? 1 : 0;
return a + b + c;
}
console.log(create(15, 20, 1))
Solution 3:
Here is a function encrypt
that works on an array of any number of arguments, as long as you provide long enough encript digits array, and inputs are non-negative integers - if any of the conditions are not met, undefined
is returned:
functiondec2bin(dec, digits) {
if(typeof(dec)=="number" && dec%1==0 && dec>=0 && dec<Math.pow(2, digits))
return dec.toString(2).padStart(digits, "0");
returnundefined;
}
functionencrypt(userDetailsArray, encriptDigitsArray) {
if(userDetailsArray.length<=encriptDigitsArray.length) {
var result=(
userDetailsArray.map(
(detail, index) =>dec2bin(detail, encriptDigitsArray[index])
)
);
if(result.includes(undefined))
returnundefined;
elsereturn result.join("");
}
returnundefined;
}
console.log(encrypt([30,13,1],[8,4,1])); /* your example */console.log(encrypt([30,13],[8,4,1])); /* less input */console.log(encrypt([30],[8,4,1])); /* even less input */console.log(encrypt([30,13,1,100,5],[8,4,1,7,4])); /* more input and encript digits */console.log(encrypt([999,13,1],[8,4,1])); /* bad input */console.log(encrypt([30,13,1],[8,4])); /* not enough encript digits */
Decrypt (without testing validity of arguments):
functiondecrypt(bin, encriptDigitsArray) {
var result=[];
while(bin!="" && encriptDigitsArray.length) {
result.push(parseInt(bin.slice(0,encriptDigitsArray[0]), 2));
bin=bin.slice(encriptDigitsArray[0]);
encriptDigitsArray.shift();
}
return result;
}
console.log(decrypt("0001111011011",[8,4,1]));
console.log(decrypt("000111101101",[8,4,1]));
console.log(decrypt("00011110",[8,4,1]));
.as-console-wrapper { max-height: 100%!important; top: 0; }
Post a Comment for "Method Implementation That Takes 3 Parameters And Returns A Binary Number"