2

I try to make an Array in javascript with number of elements and elements inputted by user through window.prompt().

This is what i got by now :

var n = window.prompt("Insert number of numbers", "")
var nr = new Array(n)
for (i = 0; i < n; i++) {
  nr[i] = window.prompt("Number: ", "")
}
for (i = 0; i < n; i++) {
  if ((i + 1) % 2 != 0) {
    if (nr[i] % 2 == 0)
      document.write("Even numbers on odd position are: ", nr[i], "<br/>")
  }
}
document.write("The numbers are :")
for (i = 0; i < n; i++) {
  document.write(nr[i] + "<br/>")
}

On web page shows nothing.

6
  • Are the numbers prompted for? What is the rest of the page this is part of? Commented May 22, 2019 at 15:40
  • You've got a number of syntax errors in your code. In particular, you're not properly closing strings or parameter lists. Check the console, it should point out to you where the problems are. Commented May 22, 2019 at 15:41
  • This is just an application, for education purpose, not a page Commented May 22, 2019 at 15:41
  • 1
    Okay, you've updated your question to fix the syntax errors. I assume the code works now? Commented May 22, 2019 at 15:43
  • look into console - seems you've got some typos in your code Commented May 22, 2019 at 15:44

1 Answer 1

3

Defining elements.

The first thing in your code is that you should define an array as this:

var numbers = [];

In this array, you will handle every element you will receive from the prompt. So, with this said, you just need the total amount of numbers that you will use which can be retrieved by doing a prompt for a number:

var times = window.prompt("Insert number of numbers: ");

So, times would be our variable containing the amount of numbers we should ask the user, which will be saved on numbers.

Asking user for numbers X amount of times.

Now, what you could do is just a simple for loop which only job is to push the new number provided by the user:

for (let i = 0; i < times; i++) {
  numbers.push(window.prompt(`Number ${i+1}: `))
}

This will give the user a prompt saying Number X:, which means the number that is being added.

Checking numbers for even and odds.

And for your functionality of giving a message when there is an even number in an odd index, you could do this:

numbers.forEach((n,i)=>{
  if(n%2===0 && i%2!=0){
    document.write(`Even number ${n} in odd position ${i} <br/>`);
  }
});

Which will check every number you received from the user and check in one inline condition if even number is in odd position and will output the line only if this condition is true.

Printing the numbers.

And simply to output every number you can do:

numbers.forEach((n)=>{
   document.write(n + "<br/>")
});

See how it works:

var times = window.prompt("Insert number of numbers"), numbers = [];
for (let i = 0; i < times; i++) {
  numbers.push(window.prompt(`Number ${i+1}: `))
}
numbers.forEach((n,i)=>{
  if(n%2===0 && i%2!=0){
    document.write(`Even number ${n} in odd position ${i} <br/>`);
  }
});

document.write("The numbers are: <br/>")

numbers.forEach((n)=>{
   document.write(n + "<br/>")
});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.