|
Member 15755181 wrote: how do I do that? Probably by writing some code.
|
|
|
|
|
You're going to have to describe, in detail, what a "campaign open window" is.
|
|
|
|
|
I am trying to number each result so that it says:
City #1 is Los Angeles.
City #2 is...
City #3 is...
... and so on.
I was thinking that I had to write something inside the for loop?
<html>
<body style="text-align: center;">
<h2 id="hi">Enter Your Five Favorite Cities</h2>
<form class="" action="index.html">
<input type="text" name="favoriteCities[]" value="" /><br>
<br>
<input type="text" name="favoriteCities[]" value="" /><br>
<br>
<input type="text" name="favoriteCities[]" value="" /><br>
<br>
<input type="text" name="favoriteCities[]" value="" /><br>
<br>
<input type="text" name="favoriteCities[]" value="" /><br>
<br>
<button type="button" name="button" onclick="favoriteCities()">Submit</button>
</form>
<h3 id="hi">Results</h3>
<p id="output"></p>
<script type="text/javascript">
var r = "";
function favoriteCities() {
var input = document.getElementsByName('favoriteCities[]');
for (var i = 0; i < input.length; i++) {
var a = input[i];
r = r + "City #" + "???" + "is " + a.value + ("<br>");
}
document.getElementById("output").innerHTML = r;
}
</script>
</body>
</html>
I am using Notepad++.
|
|
|
|
|
You already have a number, which you're using as the index in the for loop. You just need to add 1 to get the numbers to start at 1 instead of 0 .
You'll also want to HTML-encode the user-supplied data so that it displays properly. The simplest way to do that is to create an element and set its innerText property.
const favoriteCities = function() {
const output = document.getElementById("output");
output.innerHTML = "";
const input = document.getElementsByName("favoriteCities[]");
for (let i = 0; i < input.length; i++) {
const div = document.createElement("div");
div.innerText = `City #${i + 1} is ${input[i].value}`;
output.appendChild(div);
}
};
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Print the following pattern using for loop
*****
****
***
**
*
*
**
***
****
*****
|
|
|
|
|
You just need a loop that counts down from 5 to 1 and then up back to 5. Give it a try. If you need specific assistance with Javascript then go to JavaScript Tutorial[^].
|
|
|
|
|
I got it! Thnx
modified 29-Aug-22 21:01pm.
|
|
|
|
|
|
You have edited every question you have ever posted to read "I got it txh", after you have been given the answer.
This is not only extremely rude, it will NOT prevent your teachers from discovering your attempt to cheat on your homework.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
export class myClass implements OnInit {
counter=0;
static counter:any;
onListItemClick(PackDef: PackDefinition): void {
this.itemClicked.emit(PackDef);
this.counter++;
console.log(this.counter);
and
<pre>import { myClass } from '../../../';
dialogRef.afterClosed().subscribe((result: any) => {
this.saveMyMethod();
console.log("Final1"),
console.log(myClass.counter);
});
saveMyMethod(): void {
console.log("Final2"),
console.log(myClass.counter);
Do you know why I cant have correct counter after "Final1" or "Final2"? but I have a correct counter in myClass.
modified 26-Jul-22 12:58pm.
|
|
|
|
|
Afi_sh wrote: counter=0;
static counter:any;
Took a look at that part real closely.
Jeremy Falcon
|
|
|
|
|
Hello Everyone,
I am in a Core Technologies class and Javascript coding is a topic that I don't fully understand. For our coding assignment, we were directed to www.w3schools.com where we could try different codes, run them and see how they work. When given the original code, I understand how to change it and I see how the modification changes what is displayed but, I don't fully understand the basic elements of the code and how to start from scratch to write one. Can anyone help me out with understanding the basic elements of a code starting from scratch or could you suggest a website that can explain it from scratch? Thank you in advance for any help you can offer.
Regards,
John
|
|
|
|
|
W3Schools Online Web Tutorials[^] is probably the best site for learning web based technologies. So go to the Javascript section and follow it through, it will explain everything.
|
|
|
|
|
|
First, welcome to web development. It's an industry that only continues to grow. We'd be happy to help with specific questions.
As far as just getting started with all the concepts, nothing beats a good book. The website linked is great, but usually book authors take much more time to explain things.
I wish I could recommend a good current book, but I started web development back in the 90s. That crap is out of print by now I'm sure. But, once you do get the concepts, I can recommend a reference website. The Mozilla foundation (same peeps that make FireFox) have an online resource called the Mozilla Developer Network (MDN).
Learn web development | MDN[^]
It will literally be one of the best reference sites to use, once you get the concepts down.
Jeremy Falcon
|
|
|
|
|
Got it! Thnx
modified 29-Aug-22 21:01pm.
|
|
|
|
|
Shouldn't the comparisons look something like,
if (a == b)
{
}
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
As you suggest, the '=' should be '=='. As is
if (randomNum = 0) sets randomNum to 0 and the 'if' effectively becomes 'if (0)' which is a falsy result (not false, but treated false), so it carries on. Then
else if (randomNum = 1) sets randomNum to 1 and the 'if' effectively becomes 'if (1)' which is a truthy result (not true, buth treated as true) so it succeeds.
Many people may jump up and down and say it shows what a dreadful language JavaScript is; but the same thing is valid in many other languages, e.g. C, C#, Java.
|
|
|
|
|
Got it thnx!
modified 29-Aug-22 21:01pm.
|
|
|
|
|
You need to show the code you have for people to help you. No one here is going to write your complete assignment.
|
|
|
|
|
function colorSelector (colorCode) {
let randomNumber = Math.floor((Math.random() * 100));
if (randomNumber < 10) {
console.log("green");
}
else if (randomNumber >= 10 && randomNumber < 19) {
console.log("yellow");
}
else if (randomNumber > 19) {
console.log("blue");
}
return (randomNumber);
}
console.log(colorCode);
This is how far I got. Im confused what should be inside the function and what should we outside. Im also not sure if im using the return operator the right way.
modified 29-Aug-22 21:01pm.
|
|
|
|
|
Your code is quite confused, and confusing. You specify that the function takes an input parameter named colorCode , but you never refer to it in the function. Your if/else clauses include the variable condition which is never defined anywhere. Finally you return the randomNumber for no reason I can see.
- The function does not need an input parameter, unless you want to pass it a random number. Assuming yo do not, then generate the number as the first step.
- The if statements only need to be as follows
- 1: If the value is less than 10 set colorCode to "green"
- 2: Else If it is less than 50 set colorCode to "yellow" (we already know it is not less than 10)
- 3: Else set it to "blue" (it must be 50 or greater)
- return colorCode to the caller of the function.
modified 16-Jul-22 7:11am.
|
|
|
|
|
So, you nearly there.
function colorSelector (colorCode) {
let randomNumber = Math.floor((Math.random() * 100));
if (randomNumber < 10) {
console.log("green");
}
else if (randomNumber >= 10 && randomNumber < 19) {
console.log("yellow");
}
else if (randomNumber > 19) {
console.log("blue");
}
return (randomNumber);
}
console.log(colorSelector(50)); I got a warning that "colorCode" doesn't exist. And true, it doesn't. That makes it confusing, because there's two options. Let's forget them for a second. Try the code I gave you, vary the number. It writes to the console also.
Member 15708114 wrote: I know that I have to use the Math.floor and Math.random to create a random number and the if and return operators but I think im messing up the order. Please help anyone. You creating a random number inside the function. You want to "pass" it to the function as an argument.
So you don't see where on thing starts and the other ends? It's called "scope". Ask your teacher. Once clear, ask to step.
It executes one line, shows result in debug. That way you see how the computer "steps" through your instructions.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
else if (randomNumber >= 10 && randomNumber < 19) {
^^^^^^^^^^^^^^^^^^
We know it is >= 10 from the previous if statement.
else if (randomNumber > 19) {
And it this point it must be > 19, so all that is needed is a simple else clause.
|
|
|
|
|
Richard MacCutchan wrote: And it this point it must be > 19, so all that is needed is a simple else clause. That's the advanced course. I somehow think his teacher might have escaped that.
It's a beginner.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|