True basic password generator [closed] - basic

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can I create a 8-digit password generator with letters(upper case characters, lower case characters), numbers and special characters using true Basic?

function generatePassword(passwordLength) {
var numberChars = "0123456789";
var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lowerChars = "abcdefghijklmnopqrstuvwxyz";
var allChars = numberChars + upperChars + lowerChars;
var randPasswordArray = Array(passwordLength);
randPasswordArray[0] = numberChars;
randPasswordArray[1] = upperChars;
randPasswordArray[2] = lowerChars;
randPasswordArray = randPasswordArray.fill(allChars, 3);
return shuffleArray(randPasswordArray.map(function(x) { return x[Math.floor(Math.random() * x.length)] })).join('');
}
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
alert(generatePassword(12));

Related

Why is the number not correct? [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 4 months ago.
Somehow I got all my numbers to save with 00 at the end. I thought I could just divide by 100 but that does not work. It always prints 1 number off. The number I am using is: 983037730529353700. I did 983037730529353700/100 and get 9830377305293538 not 9830377305293537.
You can use substring() function or slice() function, to remove the 2 extra 0s on the end. To do it:
Using substring() function:
if(!args[0]) { //Preventing for an error if args[0] not given
const embed = new MessageEmbed()
.addField("To use these command, type:", `${prefix} <Some kind of ID>`)
.setTimestamp()
.setColor('RANDOM')
message.channel.send({embeds: [embed]})
} else {
var str = args[0];
str = str.substring(0, str.length - 2);
message.channel.send(`${str}`)
}
var str = "983037730529353700";
str = str.substring(0, str.length - 2);
console.log(str)
Using slice() function:
if(!args[0]) {
const embed = new MessageEmbed()
.addField("To use these command, type:", `${prefix} <Some kind of ID>`)
.setTimestamp()
.setColor('RANDOM')
message.channel.send({embeds: [embed]})
} else {
var str = args[0];
str = str.slice(0, -2);
message.channel.send(`${str}`)
}
var str = "983037730529353700";
str = str.slice(0, -2);
console.log(str)

How to convert string and integer to binary in nodejs?

I have the following problems. I have an integer and a string. Both of them need to be converted into binary format. For the integer I found a solution that, as far as I can tell, works. The string on the other hand, I don't have a solid understanding of it.
String(16), as far as I understand, means something like Array<UInt8> and has a fixed length of 16. Am I correct? If so, is there a better way to converting them by hand built in in NodeJS?
const myNumber = 2
const myString = 'MyString'
const myNumberInBinary = toUInt16(myNumber) // eg. 0000000000000101
const myStringinBinary = toString16(myString) // I honestly don't know
function toUInt16(number) {
let binaryString = Number(number).toString(2)
while (binaryString.length < 16) {
binaryString = '0' + binaryString
}
return binaryString
}
// TODO: implement
function toString16(string) {
...
return binaryString
}
best regards
EDIT:
Thanks for all the comments and the answer. They helped me understand this process better. The solution I ended up with is this one:
const bitString = "00000101"
const buffer = new Buffer.alloc(bitString.length / 8)
for (let i = 0; i < bitString.length; i++) {
const value = bitString.substring(i * 8, (i * 8) + 8)
buffer[i] = Number(value)
}
fs.writeFileSync('my_file', buffer, 'binary')
Thanks again!
You should loop through the string and do this on each character:
let result = ""
for (let i = 0; i < myString.length; i++) {
result += myString[i].charCodeAt(0).toString(2) + " ";
}

From plain text to TEX - Javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm building a math application and I want to show math formulas written with a good design in javascript (nodejs). So I would transorm plain text to TEX format.
From formula like this:
(25*5)/6
To formula like this:
\frac{(25 \cdot 5)}{6}
To good design:
If there are other known methods you advise me.
Many thanks in advance!
I've rewritten this expression parser from java to javascript. not the most straightforward way. Note that this is not extensively tested.
function texFromExpression(str){
var pos = -1, ch;
function nextChar(){
ch = (++pos < str.length) ? str.charAt(pos) : -1;
}
function eat(charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
function parse(){
nextChar();
var x = parseExpression();
if (pos < str.length) throw `Unexpected: ${ch}`
return x;
}
function parseExpression() {
var x = parseTerm();
for (;;) {
if (eat('+')) x = `${x} + ${parseTerm()}` // addition
else if (eat('-')) x = `${x} - ${parseTerm()}` // subtraction
else return x;
}
}
function parseTerm() {
var x = parseFactor();
for (;;) {
if (eat('*')) x=`${x} \\cdot ${parseTerm()}`; // multiplication
else if (eat('/')) x= `\\frac{${x}}{${parseTerm()}}`; // division
else return x;
}
}
function parseFactor() {
if (eat('+')) return `${parseFactor()}`; // unary plus
if (eat('-')) return `-${parseFactor()}`; // unary minus
var x;
var startPos = pos;
if (eat('(')) { // parentheses
x = `{(${parseExpression()})}`
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = str.substring(startPos, pos);
} else if (ch >= 'a' && ch <= 'z') { // variables
while (ch >= 'a' && ch <= 'z') nextChar();
x= str.substring(startPos, pos);
if(x.length>1){
x = `\\${x} {${parseFactor()}}`;
}
} else {
throw `Unexpected: ${ch}`
}
if (eat('^')) x = `${x} ^ {${parseFactor()}}` //superscript
if(eat('_')) x = `${x}_{${parseFactor()}}`;
return x;
}
return `$${parse()}$`;
}

Clear list each cell end edit event [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here I am search by comboboxselected value as search setting and item id
problem appear only when change combobox item and search by another setting get 'item not found' although am sure its exist but not in current list.
if (e.ColumnIndex == 0)
{
List<ComboUnit> ItemUnitsList = new List<ComboUnit>();
ComboUnit ItemUnitsObj = new ComboUnit();
long x = Convert.ToInt64(dataGridView1.Rows[i].Cells[0].Value);
string r = ddlInvSearchSet1.SelectedValue.ToString();
TechTouch.Methods o = new TechTouch.Methods();
List<db.Vitems> itemDetails = o.SearchItem(x, r);
if (itemDetails.Count == 0)
{
{
MessageBox.Show("NO Items Found");
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
// dataGridView1.Rows[i].Cells [2]= dataGridView1.Columns[2].Index + 1;
dataGridView1.BeginEdit(true);
dataGridView1.AllowUserToAddRows = false;
}
itemDetails.Clear();
dataGridView1.Refresh();
}
else
{
foreach (var item in itemDetails)
{
ItemUnitsObj.UnitName = item.UnitNameArabic;
ItemUnitsObj.UnitId = item.UnitId;
ItemUnitsList.Add(ItemUnitsObj);
ItemUnitsObj = new ComboUnit();
dataGridView1.Rows[i].Cells[1].Value = itemDetails.First().NameArabic;
if (itemDetails.Count > 1)
{
DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
comboBoxCell.DataSource = ItemUnitsList;
comboBoxCell.DisplayMember = "UnitName";
comboBoxCell.ValueMember = "UnitId";
dataGridView1.Rows[i].Cells[2] = comboBoxCell;
}
else
{
DataGridViewTextBoxCell textBoxCell = new DataGridViewTextBoxCell();
textBoxCell.Value = itemDetails.First().UnitNameArabic;
dataGridView1.Rows[i].Cells[4].Value = itemDetails.First().SalePrice;
dataGridView1.Rows[i].Cells[2] = textBoxCell;
dataGridView1.Rows[i].Cells[7].Value = itemDetails.First().UnitId;
dataGridView1.Refresh();
}
}
}
itemDetails.Clear();
}
Add itemDetails.Clear(); Inside IF statement
if (itemDetails.Count == 0)
{
MessageBox.Show("NO Items Found");
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
dataGridView1.Rows[i].Cells[0].Value = "";
dataGridView1.BeginEdit(true);
itemDetails.Clear();
}
else
{
}

Amazon puzzle asked in initial screening test [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
Amazon can sell products in packs of 6,9 and 20. Given a number N, give the algorythm to find its possible or not.
Ex - 21 -- Not Possible
47 (9*3+20) -- Possible
A very simple, but recursive, solution would be
private bool isDiv(int n) {
if (n < 6) {
return false;
} else if (n == 6 || n == 9 || n == 20) {
Console.Write(n);
return true;
} else if (isDiv(n - 6)) {
Console.Write(" + " + 6);
return true;
} else if (isDiv(n - 9)) {
Console.Write(" + " + 9);
return true;
} else if (isDiv(n - 20)) {
Console.Write(" + " + 20);
return true;
}
return false;
}
But keep in mind that this is not the fastest solution - but it works good for small numbers like the ones in your example
Oh and 21 is quite possible:
2*6 + 9 = 21
A very simple logical solution using C
#include <stdio.h>
int main(){
int x = 6, y=9, z=20,n=26,t1,t2,i;
t1 = n%z;
for(i=0; i<2;i++)
{
if (t1 < y)
{
t2 = t1%x;
}
else
{
t2 = t1%y;
t1 = t2;
}
}
if (t2 == 0 || t1 == 0)
{
printf("Yes we can do it");
}
else
{
printf("we cannot do it");
}
}

Resources