2017-08-28 54 views
1

問題: 創建一個程序,提示用空格分隔的數字列表。讓程序打印出一個只包含偶數的新列表。僅打印偶數

將輸入轉換爲(數組)。許多語言可以很容易地將字符串轉換爲具有內置函數的數組,該函數根據指定的分隔符分割字符串。 編寫您自己的算法 - 不要依賴於語言的內置過濾器或類似的枚舉功能。 使用名爲「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); 
+0

好的...那麼問題在哪裏?你有什麼嘗試? – Dekel

+2

我們不是來做你的功課。 – Ronnie

+1

這看起來非常像功課。爲什麼不刺激自己實現所需的邏輯,如果你陷入困境,那麼請尋求幫助。你永遠不知道,也許你甚至會學到一些東西。 – Dan

回答

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]); 
} 
} 

這是發現和推動偶數到另一個數組的數組的例子。你可以很容易地改變它,它不會將它推到另一個數組,而是會打印偶數。它會幫助你解決你的問題

0

有沒有你不明白的問題的一部分?你有什麼麻煩?

爲了提示用戶輸入,您可以使用window.prompt('Enter a list of numbers separated by spaces'); 這種接收用戶輸入的方式將返回一個字符串。既然你不能使用內置的方法來把這個變成一個列表,你可以採取一種方法是:

  1. 存儲用戶輸入通過串中的每個字符變量
  2. 迭代。定義一個變量currentInteger,該變量用於保存字符串中當前正在查看的數字的數字(請記住:數字可以長於一個數字,因此,在迭代字符串時,當前數字可能不會由單個字符表示正在看)
  3. 一旦你到達一個空間,你知道currentInteger已完成,追加到一個新的列表,如果它是一個偶數。

由於您的currentInteger變量是一個字符串,您將需要使用parseInt()來使它成爲一個數字並檢查它是否是偶數。