問題: 創建一個程序,提示用空格分隔的數字列表。讓程序打印出一個只包含偶數的新列表。僅打印偶數
將輸入轉換爲(數組)。許多語言可以很容易地將字符串轉換爲具有內置函數的數組,該函數根據指定的分隔符分割字符串。 編寫您自己的算法 - 不要依賴於語言的內置過濾器或類似的枚舉功能。 使用名爲「filterEvenNumbers」的函數來封裝此邏輯。該函數接受舊數組並返回新數組。
所有我對這個筆記:
//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);
好的...那麼問題在哪裏?你有什麼嘗試? – Dekel
我們不是來做你的功課。 – Ronnie
這看起來非常像功課。爲什麼不刺激自己實現所需的邏輯,如果你陷入困境,那麼請尋求幫助。你永遠不知道,也許你甚至會學到一些東西。 – Dan