おいーす、もりたけです。
フリーコードキャンプを日本語で解説!の第61弾です。
JavaScript Algorithms and Data Structures Certification (300 hours)の中の、
Basic JavaScriptの中の、
Comparison with the Greater Than Operator
です。
さっそく見ていきましょう。
まずは本文から。
The greater than operator (
>
) compares the values of two numbers. If the number to the left is greater than the number to the right, it returnstrue
. Otherwise, it returnsfalse
.Like the equality operator, greater than operator will convert data types of values while comparing.Examples 5 > 3 // true7 > ‘3’ // true2 > 3 // false‘1’ > 9 // falseAdd the greater than operator to the indicated lines so that the return statements make sense.
解説していきます。
今回は比較演算子のひとつ、大なり(>)についてです。
>は左辺と右辺を比べて、左辺の方が小さければtrueを返し、右辺の方が大きければfalseを返します。
例を見てみましょう。
5 > 3 // true
7 > ‘3’ // true
2 > 3 // false
‘1’ > 9 // false
中学生の時に習った数学と同じです!
では課題をみていきます。
>を使って返ってくる文字列が正しいくなるようにしろ。という課題です。
valの値が100よりも大きいとと”Over 100″
valの値が10よりも大きいと”Over 10″
valの値が10以下だと”10 or Under”
と返ってくればOKです。
if文の条件チェックの箇所でval > 100 , val > 10 と書いてあげれば良さそうですね。
答えはこうです。
function testGreaterThan(val) {
if (val > 100) { // Change this line
return “Over 100”;
}
if (val > 10) { // Change this line
return “Over 10”;
}
return “10 or Under”;
}
// Change this value to test
testGreaterThan(10);
という事で今回は>について学びました。
では次回!