Reputation: 25
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.com/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.com/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.com/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);
Upvotes: 1
Views: 6700
Reputation: 9
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 :
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)currentInteger
has been completed, 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.
Upvotes: 0
Reputation: 99
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
Upvotes: 4