create JavaScript program for both task in html coding
Assignment #10 – Arrays
Task#1 – Favourite Places
Ask the user to enter ten of their places they have visited in their life. Once they have entered there favourite places, display the results. Use an array to store the ten favourite places.
Task#2 – Favourite Actor
Ask the user to enter their favourite actor and the role that the actor played. Save the actors in one array and the roles in another array. Ask for four favourite actors. Then ask the user for one of their favorite actors. Find the role that the actor played and display it to the screen.
Task#1 – Favourite Places
<!DOCTYPE html>
<html>
<head>
<script>
// an array to store the ten favourite places.
var favouritePlaces = [];
for (i = 0; i < 10; i++) {
//get the favourite place
favouritePlaces.push(prompt("Enter your favourite place "+(i+1)+": "));
}
window.onload = function () {
document.getElementById("favouritePlaces").innerHTML =favouritePlaces;
};
</script>
</head>
<body>
<p id="favouritePlaces"></p>
</body>
</html>
Task#2 – Favourite Actor
<!DOCTYPE html>
<html>
<head>
<script>
// Save the actors in one array and the roles in another array.
var favouriteActors = [];
var favouriteRoles = [];
// Ask for four favourite actors.
for (i = 0; i < 4; i++) {
//get favourite actor and the role that the actor played.
favouriteActors.push(prompt("Enter your favourite actor " + (i + 1) + ": "));
favouriteRoles.push(prompt("Enter your favourite role that the actor played " + (i + 1) + ": "));
}
//Then ask the user for one of their favorite actors.
var favoriteActorTarget=prompt("Enter your favourite actor to search: ");
//Find the role that the actor played and display it to the screen.
var roleIndex=-1;
for (i = 0; i < 4; i++) {
if(favoriteActorTarget==favouriteActors[i]){
roleIndex=i;
}
}
window.onload = function () {
//Find the role that the actor played and display it to the screen.
if(roleIndex!=-1){
document.getElementById("role").innerHTML = "The role the actor played is: "+favouriteRoles[roleIndex];
}else{
document.getElementById("role").innerHTML = "The actor is not found.";
}
};
</script>
</head>
<body>
<p id="role">
</p>
</body>
</html>
Comments
Leave a comment