Last active
May 25, 2017 07:53
-
-
Save satoyuichi/dcf1ea324d1372d83d30acb5c118593b to your computer and use it in GitHub Desktop.
関数の例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function outputString (/* 入力 */) { | |
let str = 'hogepiyo'; // 処理 | |
return str; // 出力 | |
} | |
// 'にゃん'の個数を指定出来るようにしなさい | |
// ex) outputStringAddNyan ('難しい', 2) | |
// '難しいにゃんにゃん' | |
function outputStringAddNyan (input, number) { | |
for (let i = 0; i < number; i++) { | |
input += 'にゃん'; | |
} | |
console.log (input); | |
} | |
// 指定した回数 'うんこ' を出力するようにしなさい | |
// ex) outputUnko (3) | |
// うんこ | |
// うんこ | |
// うんこ | |
function outputUnko (number) { | |
let str = ''; | |
for (let i = 0; i < number; i++) { | |
str += 'うんこ'; | |
} | |
return str; | |
} | |
// 文字列の長さを返す関数を作りなさい | |
// ex) functionName ('abcdef'); | |
// 6 | |
// 文字列を引数にとる関数を作る | |
function getStringLength (str) { | |
let count = 0; | |
// 文字列の中身を列挙する | |
for (let elem of str) { | |
// 文字の数を数える | |
count++; | |
} | |
return count; | |
} | |
// 関数を呼び出す | |
let length = getStringLength ('hogehogepiyopiyo'); | |
// 文字列の長さを出力する | |
console.log (length); | |
// 文字列を反転して返す関数を作りなさい | |
// ex) functionName ('abcdef'); | |
// fedcba | |
function reverseString (mojiretsu) { | |
let answer = ''; | |
for (let x of mojiretsu) { | |
answer = x + answer; | |
} | |
return answer; | |
} | |
console.log (reverseString ('baikinman')); | |
function outputEachElement (str) { | |
for (let elem of str) { | |
console.log (elem); | |
} | |
} | |
outputEachElement ('abcdef'); | |
outputEachElement ('hogepiyo'); | |
outputEachElement ('aiueo'); | |
function rejectSpace (input) { | |
let output = ''; | |
// 文字列を列挙する | |
for (let elem of input) { | |
// 列挙した文字がスペースかどうか判別する | |
if (elem !== ' ') { | |
// スペースでなかったら、拾う | |
output += elem; | |
} | |
// else { | |
// // スペースだったら、捨てる | |
// } | |
} | |
return output; | |
} | |
// 関数を呼び出す | |
let result = rejectSpace ('ho ge ho ge piyo pi yo'); | |
// 結果を出力する | |
console.log (result); | |
// 配列の最大値を返す関数 | |
function getMaxFromArray (array) { | |
let max = array[0]; | |
for (let value of array) { | |
if (max < value) { | |
max = value; | |
} | |
} | |
return max; | |
} | |
let maxValue = getMaxFromArray ([39, 482, -12, 0, 32, 3, 40392, 21]); | |
console.log (maxValue); | |
// 文頭のスペースを取り除く | |
function trimHeadSpace (input) { | |
let output = ''; | |
let isHeadSpace = true; | |
for (let elem of input) { | |
if (elem !== ' ') { | |
isHeadSpace = false; | |
} | |
if (isHeadSpace === false) { | |
output += elem; | |
} | |
} | |
return output; | |
} | |
// 文字列の文頭文末からスペースを取り除く関数 | |
function trimString (input) { | |
let output; | |
output = trimHeadSpace (input); | |
output = reverseString (output); | |
output = trimHeadSpace (output); | |
output = reverseString (output); | |
return output; | |
} | |
let result = trimString (' hoge hoge piyopiyo '); | |
console.log (result); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment