• +1 240 877 8161
  • info@thePracticalIT.com
Practical Information Technology
  • Notifi
  • Canvas
    • HOME
    • Start Here
    • Courses
    • EVENTS
      • EVENTS LIST
      • EVENTS GRID
    • BLOG
    • CONTACT
    • IT Courses
  • +1 240 877 8161
  • info@thePracticalIT.com
  • Profile
  • Login | Registration Popup Link
Practical Information Technology
  • Home
  • Courses
    • FEATURED COURSES
      • Web Development
      • Database Development
      • Business Intelligence
    • TRAINING
      • How long does it take?
      • IT Training Methods
      • Hours Per week
      • Payments
    • IT Courses
  • Tips
  • Contact
  • Notifi
  • Start Here

Accept Numbers And Populate Array

Homepage - Programming Exercises - Accept Numbers And Populate Array

Pract IT

by Practical IT
Posted on 26 May, 2021
in
Programming Exercises
0 Comment

Use a prompt("Give me your favorite numbers") method to accept number from user and put them to array variable called favorite_numbers until the user enters 0. The program stops asking and exits after the user enters 0.

What is expected

By going through this problem, you will perfectly understand the usage of do.. while looping in that there is a need at least to ask for the number at least one time. It can also be done with while of course, but do..while is the perfect one for this.

Hint: you will need to use .push method to add the numbers to the array. It is advisable to use parseInt() method to convert the accepted values to numbers also.

Show Answer

var favorite_numbers = []; //what is the difference between var and let?
var number = null; //is it better to declare number in do {} body?
do {
    number = prompt("Please give me your favorite number");
    number = parseInt(number); //same as parseInt(prompt("Please.."));
    if (number != 0) {
        favorite_numbers.push(number);
    }
} while (number != 0);

Tags: adding numbers arrays do while push
User Avatar
Article by Practical IT

Works at Practical IT. Passionate about Technologies, Business and News.

Previous Story
Counting Numbers Above 20
Next Story
Function to Return Summation of Odd Numbers

Related Articles

Counting Numbers Above 20

For the following array containing numbers, count how many numbers...

Leave your comment Cancel Reply

(will not be shared)

SearchLeave your keyword

Change your life through Information Technology career today. Thousands have done it – it is your turn now. All you need is a passion to change your life and provide better for yourself and your family. Practical IT takes the rest.

QUICK LINKS

  • Home
  • Courses
    • FEATURED COURSES
      • Web Development
      • Database Development
      • Business Intelligence
    • TRAINING
      • How long does it take?
      • IT Training Methods
      • Hours Per week
      • Payments
    • IT Courses
  • Tips
  • Contact
  • Notifi
  • Start Here

SUBSCRIBE EMAIL

Subscribe to our email list and get tips on IT jobs, career changes openings, seminars and more. We can also SMS you

GET IN TOUCH

You can call us, email us or even better visit us in our office at the heart of Silver Spring. Our Email - info@thePracticalIT.com

+1 240 877 8161

911 Silver Spring Ave #101 Silver Spring MD 20910

Practical IT - Copyright 2023.
  • Sign in

or
  • Login with Facebook
  • Login with Google

Forgot your password?

Lost your password? Please enter your email address. You will receive mail with link to set new password.

Back to login