zuppachu / Joanne-s-Learning-Blog

程式導師實驗計畫第二期 之 Coding 學習紀錄。
2 stars 0 forks source link

[ FCC ] - JavaScript #11

Open zuppachu opened 5 years ago

zuppachu commented 5 years ago

二維陣列 two-dimensional

先說陣列是什麼? 陣列,又稱數組,為一資料結構(Data Structure)。而陣列內每一個元素,會有一個類似編號的東西,稱為索引(index)。

arr = [a,b,c]
//對應序號為0,1,2

那二維陣列呢? 利用行(column,方向為 |)與列(row,方向為 一 )的方式,將資料依序存取到記憶體位置裡(memory location)。

因為多了一個維度,需要利用兩個索引值來對應陣列內的元素。 可以看 這邊 裡面的圖示。或是 FCC 論壇的解釋。 總之,第 i 列 、第 j 行的元素,表示為 a[i] [j]

zuppachu commented 5 years ago

Nesting For Loops

Modify function multiplyAll so that it multiplies the product variable by each number in the sub-arrays of arr.

function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line

  // Only change code above this line
  return product;
}

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);

ANS:

function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line

  for (var i = 0; i < arr.length; i++) {
    for (var x = 0; x < arr[i].length; x++) {
      product *= arr[i][x];
    }
  }

  // Only change code above this line
  return product;
}

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
zuppachu commented 5 years ago

查檔案用法

Profile Lookup

We have an array of objects representing different people in our contacts lists.

A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.

The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact.

If both are true, then return the "value" of that property.

If firstName does not correspond to any contacts then return "No such contact"

If prop does not correspond to any valid properties then return "No such property"

//Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["Javascript", "Gaming", "Foxes"]
    }
];

function lookUpProfile(firstName, prop){
// Only change code below this line

// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

ANS:

for (var x = 0; x < contacts.length; x++){          
    if (contacts[x].firstName === name) {          
        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}
return "No such contact";
  1. for loop掃全部的項目
  2. 選取contacts個小項裡面的firstName => contacts[x].fisrtName
  3. .hasOwnProperty() 幫助確認是否真的有 property 且返回 boolean

Math.random()

Generate Random Fractions with JavaScript

Change randomFraction to return a random number instead of returning 0.

function randomFraction() {

  // Only change code below this line.

  return 0;

  // Only change code above this line.
}

ANS:

function randomFraction() {

  // Only change code below this line.
    var result = 0;
    while (result === 0) {
        result = Math.random();
    }

    return result;

  // Only change code above this line.
}
zuppachu commented 5 years ago

Generate Random Whole Numbers within a Range

題目:

Instead of generating a random number between zero and a given number like we did before, we can generate a random number that falls within a range of two specific numbers.

To do this, we'll define a minimum number min and a maximum number max.

Here's the formula we'll use. Take a moment to read it and try to understand what this code is doing:

Math.floor(Math.random() * (max - min + 1)) + min

一開始不太懂為什麼後面要 +1

function randomRange(myMin, myMax) {

  return Math.floor(Math.random()*(myMax -myMin +1)+ myMin); // Change this line
}

// Change these values to test your function
var myRandom = randomRange(5, 15);

查到這篇文章裏頭講解的很清楚。

Math.random() :產生 0~1 之間的數字 (最小 0.0000789... ; 最大 0.99999....)

想要取得整數時可以搭配:

來看下想要 0 到 X 之間的整數數字怎麼弄?

Math.floor(Math.random()*2); 
//回傳0或1

Math.floor(Math.random()*3);
//回傳0或1或2

Math.floor(Math.random()*5); 
//回傳0或1或2或3或4

Math.floor(Math.random()*50); 
//回傳0或1或2或3...或49

上面函式寫法:

function getRandom(x){
    return Math.floor(Math.random()*x);
};
getRandom(3); //會回傳0~2之間的隨機數字
getRandom(5); //會回傳0~4之間的隨機數字

那若是想要 1 到 X 之間的整數呢?

function getRandom(x){
    return Math.floor(Math.random()*x)+1;
};
getRandom(3); //會回傳1(0+1)~3(2+1)之間的隨機數字
getRandom(5); //會回傳1(0+1)~5(4+1)之間的隨機數字

那想得兩數之間的隨機整數呢?(非 1 到 X 數之間)

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; 

  //The maximum is exclusive and the minimum is inclusive
 //最小值包括,最大值不包括

}

getRandomInt(2,10)

又,兩數之間的隨機整數,但是包括最小值和最大值?

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; 

  //The maximum is inclusive and the minimum is inclusive 
 //包括最大值和最小值。
}
zuppachu commented 5 years ago

Palindrome Checker

一開始想說有寫過迴文,不就是 str.split('').reverse().join('') 這三寶。後來題目看清楚後(是說我每次都覺得對方文意表達不清,殊不知只是自己的英文很爛而已 TT),題目要求下列幾點:

  1. 不要有特殊符號
  2. 比較時用全部都大寫或全部小寫。

刪去特殊符號這個我不清楚,上網查這個解答有下面兩種方式:

\W removes all non-alphanumeric characters:

\W matches any non-word character
\W is equivalent to [^A-Za-z0–9_]
\W matches anything that is not enclosed in the brackets

What does that mean?

[^A-Z] matches anything that is not enclosed between A and Z
[^a-z] matches anything that is not enclosed between a and z
[^0-9] matches anything that is not enclosed between 0 and 9
[^_] matches anything that does not enclose _

但因為題目要求 ('_eye') 回傳 true,所以:

We will need to add “_” to pass this specific test case.
=> We now have “\W_”

We will also need to add the g flag for global search.
=> We finally have “/[\W_]/g”