最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - print out only even numbers - Stack Overflow

programmeradmin2浏览0评论

The Problem: Create a program that prompts for a list of numbers, separated by spaces. Have the program print out a new list containing only the even number.

Convert the input to (a array). Many languages can easily convert strings to arrays with a built in function that splits apart a string based on a specified delimiter. Write your own algorithm- don't rely on the language's built-in filter or similar enumeration feature. Use a function called "filterEvenNumbers" to encapsulate the logic for this. The function takes in the old array and returns the new array.

All my notes on this:

//global array
    var arr = [];

    var arr = prompt("Enter your numbers");
    // var eachNumber = arr.split(",");
    var res = arr.split("");        
    console.log(arr);
    console.log(res);


    if(res = )

    // var str = "How are you doing today?";

    //push elements into array
    // arr.push(prompt("Enter in a bunch of numbers", "")); //push input to array
    // console.log(arr);

    // 
    // var arr = prompt("Enter your numbers").split(",");
    // console.log(arr);
    // var arr = [];
    // for(var i = 0; i < 10; i++)
    // arr.push(prompt("Enter a number");




    // Convert number into array in Javascript
    // 
    // var numbers = "1, 2, 3";
    // var eachNumber = numbers.split(",");
    // /* now parse them or whatso ever */
    // console.log(eachNumber);   




// JavaScript Array filter
// 
// The JavaScript Array filter method iterates over each value of an array passing it to a callback function. 
// If the callback function returns true, the current value is then pushed into the resulting array.
// The callback function is invoked with three arguments: the value of the element, the index of...
// the element and the Array object being traversed.        

// Bellow is an example of filtering odd and even numbers out of an array:
    // var arr = [1, 2, 3, 4, 5];
    // var odd = arr.filter(function(val) {
    //     return 0 != val % 2;
    // });
    // // odd = [1, 3, 5]
    // var even = arr.filter(function(val) {
    //     return 0 == val % 2;
    // });
    // even = [2, 4]
    // console.log(even);

// The Array filter method can also be used to remove empty, null or undefined elements from an array:

    // var arr = [0, null, 42, undefined, "", true, false, NaN, "", "foo bar"];
    // var filteredArr = arr.filter(function(val, num) {
    //     return !(val === "" || typeof val == "undefined" || val === null );
    // });
    // // // filteredArr = [0, 42, true, false, NaN, "foo bar"]

    // console.log(filteredArr);

The Problem: Create a program that prompts for a list of numbers, separated by spaces. Have the program print out a new list containing only the even number.

Convert the input to (a array). Many languages can easily convert strings to arrays with a built in function that splits apart a string based on a specified delimiter. Write your own algorithm- don't rely on the language's built-in filter or similar enumeration feature. Use a function called "filterEvenNumbers" to encapsulate the logic for this. The function takes in the old array and returns the new array.

All my notes on this:

//global array
    var arr = [];

    var arr = prompt("Enter your numbers");
    // var eachNumber = arr.split(",");
    var res = arr.split("");        
    console.log(arr);
    console.log(res);


    if(res = )

    // var str = "How are you doing today?";

    //push elements into array
    // arr.push(prompt("Enter in a bunch of numbers", "")); //push input to array
    // console.log(arr);

    // https://stackoverflow./questions/28252888/javascript-how-to-save-prompt-input-into-array
    // var arr = prompt("Enter your numbers").split(",");
    // console.log(arr);
    // var arr = [];
    // for(var i = 0; i < 10; i++)
    // arr.push(prompt("Enter a number");




    // Convert number into array in Javascript
    // https://stackoverflow./questions/20730360/convert-number-into-array-in-javascript
    // var numbers = "1, 2, 3";
    // var eachNumber = numbers.split(",");
    // /* now parse them or whatso ever */
    // console.log(eachNumber);   




// JavaScript Array filter
// http://www.diveintojavascript./core-javascript-reference/the-array-object/array-filter
// The JavaScript Array filter method iterates over each value of an array passing it to a callback function. 
// If the callback function returns true, the current value is then pushed into the resulting array.
// The callback function is invoked with three arguments: the value of the element, the index of...
// the element and the Array object being traversed.        

// Bellow is an example of filtering odd and even numbers out of an array:
    // var arr = [1, 2, 3, 4, 5];
    // var odd = arr.filter(function(val) {
    //     return 0 != val % 2;
    // });
    // // odd = [1, 3, 5]
    // var even = arr.filter(function(val) {
    //     return 0 == val % 2;
    // });
    // even = [2, 4]
    // console.log(even);

// The Array filter method can also be used to remove empty, null or undefined elements from an array:

    // var arr = [0, null, 42, undefined, "", true, false, NaN, "", "foo bar"];
    // var filteredArr = arr.filter(function(val, num) {
    //     return !(val === "" || typeof val == "undefined" || val === null );
    // });
    // // // filteredArr = [0, 42, true, false, NaN, "foo bar"]

    // console.log(filteredArr);
Share Improve this question edited Aug 28, 2017 at 23:50 Walter Purvis asked Aug 28, 2017 at 23:45 Walter PurvisWalter Purvis 252 silver badges6 bronze badges 7
  • Ok... so where is the problem? What have you tried? – Dekel Commented Aug 28, 2017 at 23:47
  • 2 We are not here to do your homework. – Ronnie Commented Aug 28, 2017 at 23:49
  • 1 This looks awfully like homework. Why not take a stab at implementing the required logic yourself, and if you get stuck then ask for help. You never know, maybe you'll even learn something. – Dan Commented Aug 28, 2017 at 23:50
  • 1 You're supposed to ask questions about code you've written, not post coding problems where others write the code for you. – RobG Commented Aug 28, 2017 at 23:50
  • This is not homework it is from a book im woking on called 57 examples for programmers. – Walter Purvis Commented Aug 28, 2017 at 23:51
 |  Show 2 more ments

2 Answers 2

Reset to default 4
var a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
b = [];

for (var i = 0; i < a.length; ++i) { 
if ((a[i] % 2) === 0) {
    b.push(a[i]);
}
}

This is an example of an array that finds and pushes the even numbers into another array. You can easily change it, and it wont push it to the another array, but instead will print even numbers. It will help you solve your problem

Is there a part of the question you do not understand? What are you having trouble with?

In order to prompt for user input you can use window.prompt('Enter a list of numbers separated by spaces'); This way of receiving user input will return a string. Since you are not allowed to use built-in methods for turning this into a list, one approach you can take is :

  1. Store the user input into a variable
  2. Iterate through each character in the string. Have a variable currentInteger defined that holds the digits of the current number you are looking at in your string (remember: numbers can be longer than a single digit, so when iterating through the string your current number may not be represented by the single character you are looking at)
  3. once you reach a space, you know that the currentInteger has been pleted, append that to a new list IF it is an even integer.

Since your currentInteger variable is a string, you will need to use parseInt() to make it a number and check if it is even.

发布评论

评论列表(0)

  1. 暂无评论