From f3bc00e668d1b986c354b68dec45a7d150d02420 Mon Sep 17 00:00:00 2001 From: Kevin CHEN <33132228+KevinZonda@users.noreply.github.com> Date: Sat, 4 Sep 2021 20:09:26 +0100 Subject: Update example code in Learn/JavaScript/Building_blocks/Looping_code, zh-CN (#2316) * sync with en-us page & fix #384 * Update index.html --- .../building_blocks/looping_code/index.html | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'files/zh-cn/learn') diff --git a/files/zh-cn/learn/javascript/building_blocks/looping_code/index.html b/files/zh-cn/learn/javascript/building_blocks/looping_code/index.html index 3fb1a217da..ab938e9c5b 100644 --- a/files/zh-cn/learn/javascript/building_blocks/looping_code/index.html +++ b/files/zh-cn/learn/javascript/building_blocks/looping_code/index.html @@ -312,21 +312,21 @@ para.textContent = info;

然后是JavaScript:

-
var contacts = ['Chris:2232322', 'Sarah:3453456', 'Bill:7654322', 'Mary:9998769', 'Dianne:9384975'];
-var para = document.querySelector('p');
-var input = document.querySelector('input');
-var btn = document.querySelector('button');
+
const contacts = ['Chris:2232322', 'Sarah:3453456', 'Bill:7654322', 'Mary:9998769', 'Dianne:9384975'];
+const para = document.querySelector('p');
+const input = document.querySelector('input');
+const btn = document.querySelector('button');
 
-btn.addEventListener('click', function(){
-  var searchName = input.value;
+btn.addEventListener('click', function() {
+  let searchName = input.value.toLowerCase();
   input.value = '';
   input.focus();
-  for (var i = 0; i < contacts.length; i++) {
-    var splitContact = contacts[i].split(':');
-    if (splitContact[0] === searchName) {
+  for (let i = 0; i < contacts.length; i++) {
+    let splitContact = contacts[i].split(':');
+    if (splitContact[0].toLowerCase() === searchName) {
       para.textContent = splitContact[0] + '\'s number is ' + splitContact[1] + '.';
       break;
-    } else {
+    } else if (i === contacts.length - 1) {
       para.textContent = 'Contact not found.';
     }
   }
-- 
cgit v1.2.3-54-g00ecf