陣列名單去除黑名單


Posted by mijouhsieh on 2022-04-01

一定要去想清楚「每一次迭代時,陣列項目到底發生了什麼變化」
視覺化工具:http://pythontutor.com/javascript.html
蒐集 2~3 種不同的作法。

  • 名單儲存在 players 變數中,準備要傳入抽獎程式。
  • Walter 和 Tim 是黑名單,要從資料中排除。
  • 注意在 players 裡出現了兩次 Walter,在刪除黑名單時,這些重覆的 Walter 都需要被處理到。
const players = ['Bernard', 'Youchi', 'Yenting', 'Angela', 'Yvonne', 'Ellen', 'Walter', 'Walter', 'Tim', 'Kevin', 'Russell']
const blackList = ['Walter', 'Tim']
// write your code
console.log(players) // should be ["Bernard", "Youchi", "Yenting", "Angela", "Yvonne", "Ellen", "Kevin", "Russell"]

產品功能

1.處理 players 陣列,確保 players 中沒有在 backList 陣列裡登記的黑名單。且如果要刪除的黑名單在 players 裡面重複出現了,那 players 中所有的黑名單也都要被刪除。
2.使用 console.log(players)印出處理後的陣列

產品規格

  1. 使用迴圈掃描陣列
  2. 使用 Array 的 splice 方法來刪除陣列項目
  3. 使用 Array 的 includes 方法來檢查,players 中是否有黑名單
  4. 請用原生 JavaScript 完成此作業

提示

使用 splice 方法時,會「直接修改原本的陣列」,造成後續項目 index 向前遞補。在這種情況下,你需要確保你真的有「掃描到 players 裡的每個項目」
設計好迴圈後,試著模擬迭代器每一輪的變化,當 Walter 被移出陣列時,Tim 的 index 變成多少?要如何確保 Tim 有被選到?

複習

flowchart

name flowchart

coding

方法一、方法二使用for loop
name for loop coding

方法三使用while
name while loop flowchart
name while loop coding


#splice #Array #For Loop #While Loop #index problem #delete blackList







Related Posts

Servlet接收前端Ajax傳遞的Json資料

Servlet接收前端Ajax傳遞的Json資料

How to solve the perpetual loading issue in Evernote? Evernote 一直轉圈圈的解決辦法

How to solve the perpetual loading issue in Evernote? Evernote 一直轉圈圈的解決辦法

API串接練習

API串接練習


Comments