Random Username Generator Javascript [closed] - javascript

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 2 years ago.
Improve this question
I'm currently using Visual Studio to build my website and I'm getting stuck on my username generator. I need it because I don't want any inappropriate words and this is a children's website so I don't want to use real information. I thought about using an array of strings and then adding them together but for some reason, I can't get it to work -__- any help would be appreciated. I'm in coding boot camp and my teacher said this would be a useful place for information. Thanks.
So far, this is what I have:
var a = ["Small", "Blue", "Ugly"];
var b = ["Bear", "Dog", "Banana"];
var rA = Math.floor(Math.random()*a.length);
var rB = Math.floor(Math.random()*b.length);
var name = a[rA] + b[rB];
alert(name);
Now my issue is I'm trying to write a function that will generate a new name with every click. I know I need function myFunction() and an onclick= event. Hopefully this is not a vague as before.

You can try something like this, (looking at comments)
var a = ["Small", "Blue", "Ugly"];
var b = ["Bear", "Dog", "Banana"];
var rA = Math.floor(Math.random()*a.length);
var rB = Math.floor(Math.random()*b.length);
var name = a[rA] + b[rB];

Related

Can i convert math.js object into JavaScript function? [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 1 year ago.
Improve this question
I'm developing simple js neural network and need to create javascript function (derivative of entered by user) before learning starts.
I know about evaluate(), but i think it'll be slowly than simple function.
That is what i want:
const derivative = math.derivative('x^2/sin(2x)', 'x');
const derivative_func = derivative.please_stay_func();
...
while(1) alert(derivate_func(12)) //:) alert result of (2 * 12 * Math.sin(2*12) - 2 * Math.pow(12, 2) * Math.cos(2*12)) / Math.pow(sin(2*12), 2)
Is it real? And maybe i'm not right about speed. Maybe there is some better ways - write here.
i am on mobile, please edit this
maybe this is want you want to do
const countThings = (customValue) ==>{
let x = 'x^2/sin(x)';
let y = 'x';
x = math.parse(x)
y = math.parse(y)
return math.derivative(x, y).evaluate({x:customValue});}
console.log(countThings(12))
and don't do while(1) alert because it will keep alerting

How do I write a test in JavaScript using the Sikuli library? [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 am looking to drive a macOS application like Calculator. I have pulled down the Sikuli jar file from Maven. I have watched the Joe C. video on this using Java and Eclipse. I am looking to do this in IntelliJ and to use JavaScript instead.
This will allow me to use my preferred IDE instead of a non-industry standard IDE like SikuliX.
This is the solution that I wrote using JavaScript and the Sikuli Script jar file available on SonaType.
var imports = new JavaImporter(java.io, java.lang, org.sikuli.script, org.sikuli.natives, org.junit);
with (imports) {
var path = "/Users/dave/IdeaProjects/Sikuli/images/";
var eight = path + "eight.png";
var multiply = path + "multiply.png";
var five = path + "five.png";
var three = path + "three.png";
var equals = path + "equals.png";
var result = path + "result.png";
var Screen = Java.type("org.sikuli.script.Screen");
var App = Java.type("org.sikuli.script.App");
var MacUtil = Java.type("org.sikuli.natives.MacUtil");
var Assert = Java.type("org.junit.Assert");
var s = new Screen();
var a = new App();
var mu = new MacUtil();
a.setName("Calculator");
mu.open(a);
s.click(eight);
s.click(multiply);
s.click(three);
s.click(equals);
Assert.assertNotNull("The Image is not Correct", s.exists(result));
}

Convert word list into array [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
I've tried to see if there were any scripts to convert a list of words into an array and I can't seem to find one.
Anyone know where I can find one?
Input:
Dog
Cat
Hamster
Gets converted to
["Dog", "Cat", "Hamster"]
Noo.. this isn't what I mean. I have a txt file with a bunch of words on each line and I was wondering if there was something out there already created that can convert each word into an array.
Just use split on the string.
For example:
var textarea = document.getElementById('list');
var arr = [];
textarea.addEventListener('input', function () {
arr = this.value.split('\n');
console.log(arr);
}, false);
Demo
If the string is actually "Dog\nCat\nHamster" then just do
"Dog\nCat\nHamster".split('\n');//returns ["Dog", "Cat", "Hamster"]
For a TextArea try this
myTextArea.value.split('\n'); // it will return desired output.
Where myTextArea is the TextArea you can get using getElement.
I think the best solution for a textarea input is using the match() function of javascript like this:
var words = []
$('textarea').on('blur', function(){
words = $(this).val().match(/\w+/g)
alert(words)
})
here a fiddle of it working.
This doesn't need for the words to be in different lines.

Need guidance to make small utility based on user inputs [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 9 years ago.
Improve this question
I want to implement a small code I am not able to understand from where to start. I want to create a text field if user enter some text into text field I want to detect category of this text. For example if user enter "How to create an app for iOs".. utlity should detect this category as information technology. another example "good hotels in Singapore" this utility should detect treat this category as Travel....
My idea to create library with words.
var categories = new Object();
categories = {
'IT' : {'iOS', 'Android'},
'Travel' : {'USA', 'London', 'Singapore'}
};
var text = 'How to create an app for iOs';
var spitter = text.split(' ');
for (var i = 0; i < spitter.length; i++) {
var word = spitter[i];
for (var categoryKey in categories) {
for (var categoryKeyWord in categories[categoryKey]) {
var regExp = new RegExp(categories[categoryKey][categoryKeyWord], i);
if (regEpx.match(word) {
//Your logic
}
}
}
}

improving my Javascript code: set a style of a link in page nav bar [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
a javascript+DOM question.
Can someone improve my code?
I have a page nav bar like this (on some pages):
<div id="pagebar">
1
2
3
4
5
6
</div>
I want my javascript to change the link style so that the style of the current page is highlighted.
My javascript code is this. It finds the current page's url, then try to match it with one of the link tag. If match, then set style.
function setPageBarStyle() {
var pageNavBar = document.getElementById("pagebar");
if (pageNavBar != null) {
var fPath = document.location.pathname;
var fPathParts = fPath.split('/');
var fName = (fPathParts[fPathParts.length-1]); // file name after last slash. e.g. xyz.html
var linkTags = pageNavBar.getElementsByTagName("a");
for (var ii = 0; ii < linkTags.length; ii++) {
var aUrl = linkTags[ii].href;
var aUrlParts = aUrl.split("/");
var aUrlLastPart = (aUrlParts[aUrlParts.length-1]); // part after last slash. e.g. xyz.html
if (aUrlLastPart == fName) {
linkTags[ii].style.border="thin solid blue";
}
}
}
}
setPageBarStyle();
How can i improve the code? I am new to DOM scripting.
The way i split the path seem too verbose, with many variables. I think it can be done with regex, but don't know how with javascript/DOM.
Thanks.
You can change this:
var aUrl = linkTags[ii].href;
var aUrlParts = aUrl.split("/");
var aUrlLastPart = (aUrlParts[aUrlParts.length-1]); // part before last slash. e.g. xyz.html
With this:
var aUrlLastPart = linkTags[ii].href.split("/").pop();
The Pop() method will return the last element of the array returned by split() and remove it from the array (which in your case, is irrelevant).
I would also recommend checking out jQuery or similars, in order to manage cross browser JS easier...
Let me know if it helps! :)

Categories

Resources