Question

In: Computer Science

/*     Here is an example of a conditional in JavaScript */ var numberOfParticipants = 5; var...

/*

    Here is an example of a conditional in JavaScript

*/

var numberOfParticipants = 5;

var maxNumberOfParticipants = 10;

if (numberOfParticipants < maxNumberOfParticipants)

{

    console.log("You can add another participant now");

}

else

{

    console.log("Sorry, you have reached the max number of participants and cannot add another");

}

/*

    Now it's your turn - create a simple conditional like my example. You can setup variables like I did or just use

    hard-coded numbers

*/








/*

    Here is an example of creating a list in JavaScript it is called an array. Here I am creating a list of my favorite songs:

*/

var myFavoriteSongs = ["Shake It Off by Taylor Swift", "12 Shots by Vic Mensa", "LSD by A$AP Rocky"];

/*

    Now it's your turn. Create an array of the colors in a rainbow (ROYGBIV)

*/









/*

    Here is an example of creating a for loop in JavaScript. There is no foreach in JavaScript so we have to do it slightly differently than

    in App Inventor. Notice how it counts from zero not one!

*/

for (var index = 0; index < myFavoriteSongs.length; index++)

{

    var favoriteSong = myFavoriteSongs[index];

    console.log((index + 1) + ") " + favoriteSong + "\n");

}

/*

    Now it's your turn. print out the array of the colors in a rainbow using a for loop

*/








/*

    Here is an example of adding an event listener to my button. Notice how the inline-function has a

    single line of coded within the {}: console.log("I was clicked!");

*/

var myButton = document.getElementById("myButton");

myButton.addEventListener("click", function () {console.log("I was clicked!");} );

/*

    Now it's your turn to do something when your button is clicked. I've created the variable "yourButton" for you already

*/

var yourButton = document.getElementById("yourButton");


Solutions

Expert Solution

Program 1

//conditional statement to check if a person can vot or not

var ageLimit = 18;

var currentAge = 12;

if (currentAge >= ageLimit)

{

    console.log("Allowed to vote");

}

else

{

    console.log("Sorry , you are too young to vote.");

}

Screenshot

Output

Program 2 and 3

Code

//array to store colours of rainbow

var colours = ["Violet","Indigo","Blue","Green","Yellow","Orange","Red"];

//itearte the array

for (var index = 0; index < colours.length; index++)

{

    var color = colours[index];

    console.log((index + 1) + ") " + color + "\n");

}

Screenshot

Output

Program 4

Code

//when clicked this will show alert dialog

var yourButton = document.getElementById("yourButton");

yourButton.addEventListener("click", function () {alert("My button was clicked");} );

Screenshot


Related Solutions

Illustrate the difference between the value-at-risk (VaR) and conditional value-at-risk (C-VaR) measures
Illustrate the difference between the value-at-risk (VaR) and conditional value-at-risk (C-VaR) measures
<script type = "text/javascript">     var  first_number = prompt("Please enter the first number for our sequence calculation");     var  second_number...
<script type = "text/javascript">     var  first_number = prompt("Please enter the first number for our sequence calculation");     var  second_number = prompt("Please enter the second number for our sequence calculation");     first_number = parseInt(first_number);     second_number = parseInt(second_number);     console.log(first_number);     console.log(second_number);     var previous_number = first_number;     var next_number = second_number;     var count = 10;     while(count>0)     {         count = count - 1;         var new_number = previous_number + next_number;         previous_number = next_number;         next_number = new_number;         console.log(next_number)     } I need to use the fib function to print the same number sequence...
Javascript Problem: List Reverse Given a list: var list = { value: 1, next: { value:...
Javascript Problem: List Reverse Given a list: var list = { value: 1, next: { value: 2, next: { value: 3, next: null } } }; Reverse the order of the list so that it looks like: var list = { value: 3, next: { value: 2, next: { value: 1, next: null } } }; Use the following shell if needed: //assignment1.js function reverseList(list) { // your code here ... return reversedList; } Example Test Case(s): Arguments: { value:...
5. Calculate the conditional probability distribution
| | a1 | a2 | |----|------|------| | b1 | 0.37 | 0.16 | | b2 | 0.23 | ? | 5. Calculate the conditional probability distribution, ?(?|?)P(A|B). 6. Calculate the conditional probability distribution, ?(?|?)P(B|A). 7. Does ?(?|?)=?(?|?)P(A|B)=P(B|A)? What do we call the belief that these are always equal? 8. Does ?(?)=?(?|?)P(A)=P(A|B)? What does that mean about the independence of ? and B?
E(Y) = 4, E(X) = E(Y+2), Var(X) = 5, Var(Y) = Var(2X+2) a. What is E(2X...
E(Y) = 4, E(X) = E(Y+2), Var(X) = 5, Var(Y) = Var(2X+2) a. What is E(2X -2Y)? b. What is Var(2X-Y+2)? c. What is SD(3Y-3X)? 2) In a large community, 65% of the residents want to host an autumn community fair. The rest of the residents answer either disagree or no opinion on this proposal. A sample of 18 residents was randomly chosen and asked for their opinions. a) What is th probability that the residents answer either disagree or...
Prove Var(|X|) <= Var(X)
Prove Var(|X|) <= Var(X)
How to validate Javascript form data? Here is the code. Can someone modify it so that...
How to validate Javascript form data? Here is the code. Can someone modify it so that all the information is validated? Thanks. <!DOCTYPE html> <html lang="en"> <head>    <title>Music Survey</title>    <meta charset="utf-8"> </head> <style>    legend { font-weight:bold;    }    </style> <body> <h1>Music Survey</h1> <form method="post" action=""> <label for="myname"><b>Name:</b></label>        <input type="text" name="myname" id="myname">        <br><br> <label for="myemail"><b>Email:</b></label>        <input type="email" name="myemail" id="myemail">        <br><br>   <fieldset> <legend>Select Your Favorite Types of Music:</legend> <input type="checkbox"...
Make a truth table with a conditional statement included. Example, ""If I own a car, then...
Make a truth table with a conditional statement included. Example, ""If I own a car, then I own a bike" Or "If I study hard, then I will pass my class" Or "If you do your homework, then you will get an allowance."
{4 marks} Here, we will quickly investigate the importance of understanding conditional prob- abilities when talking...
{4 marks} Here, we will quickly investigate the importance of understanding conditional prob- abilities when talking with medical patients. This problem is based on a true investigation by Hoffrage and Gigerenzer in 1996. The investigators asked practicing physicians to consider the following scenario: “The probability that a randomly chosen woman age 40-50 has breast cancer is 1%. If a woman has breast cancer, the probability that she will have a positive mammogram is 80%. However, if a woman does not...
var selectedColor = document.getElementById("colorChoice").value; var selectedStyle = document.getElementById("style").value; var selectedSize = document.getElementById("size").value; var orderMessage = "Your...
var selectedColor = document.getElementById("colorChoice").value; var selectedStyle = document.getElementById("style").value; var selectedSize = document.getElementById("size").value; var orderMessage = "Your order is: Color = " + selectedColor + "; Style = " + selectedStyle + "; Size = " + selectedSize+"\n"; if (selectedStyle==="Long-sleeve") //Add code to calculate the price and concatenate it to the orderMessage //For example, "The order is: Color = gray; Style=T-Shirt; Size = XL \nThe price is $12" //If the style and size are not available, then concatenate the message that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT