-1
This is my first question here on the site, so sorry if my way of questioning does not follow the expected pattern rs. I’m taking a programming course Javascript for beginners to apply for a scholarship. In one of the proposed exercises (prerequisite to enter the contest) was drawn up the following challenge:
Program a movie function that receives three arrays with the names of characters, movies and year of debut in the cinema. The role should also receive an id value chosen by the user with a range of 1 up to the maximum size of the arrays provided and return a phrase with the following template: "character is a character from the film that debuted in the cinema at release." If the id value is invalid, the function should return the phrase "This is not a valid option."
Example:
characters = ["Hermione", "Trinity", "Read"]
movies = ["Harry Potter", "Matrix", "Star Wars"]
lancings = [2001, 1999, 1977]
id = 3
You must return the string:
Leia is a character from the movie Star Wars which premiered in the cinema in 1977
The function, as well as the arrays, are already given by the developer himself and are inserted in the code of the machine itself, it is up to me to just complete it from the line that says "Write down your code".
In order to answer it I wrote the following code:
function filme(personagens, filmes, lancamentos, id)
{
// Escreva abaixo o seu código:
if (id < 1 || id > personagens.length) {
return ("Essa não é uma opção válida.")
} else {
return (personagens[id--] + ' é um personagem do filme ' + filmes[id--] + ' que estreou no cinema em ' + lancamento[id--])
}
}
However when putting it to run, the system returns error of my answer. More precisely, this is what presents me:
An invalid id must return an error message
According to him, my code is not returning the error message when an invalid ID is entered, as required by the statement. But I don’t see how that’s possible once I’ve defined through parole if the conditions under which he must do what the question asks of me.
Someone there can argue where is the error of my logic.
At once I thank you for your cooperation !
Obs: remembering that this is a question for beginners and therefore does not contain more elaborate solutions, basically I must solve the issue using the conditional if/Else, variables, arrays and nothing more.
You decrement twice id on the Return line, this in itself invalidates the post as a typo. Another thing, specific requirements of third-party websites (many are very doubtful) end up disrupting the posting. Here it is always important a [mcve] of the problem, focusing on a point doubt, and not exercise statements. Stack Overflow Survival Guide in English explains this much better and can help in formulations of upcoming posts.
– Bacco