From ff422ca42f0e4722bc4624a9f46ca177c91b7511 Mon Sep 17 00:00:00 2001 From: Matthew Francis Ong Date: Fri, 22 Sep 2017 09:49:19 +0800 Subject: [PATCH] Function: done! --- js/filterLongWords.js | 23 ++++++++++++++++++++++ js/fizzbuzz.js | 15 +++++++++++++++ js/grade.js | 40 ++++++++++++++++++++++++++++++++++++++ js/phonebook.js | 45 +++++++++++++++++++++++++++++++++++++++++++ js/reverse.js | 25 ++++++++++++++++++++++++ readme.md | 4 +--- 6 files changed, 149 insertions(+), 3 deletions(-) diff --git a/js/filterLongWords.js b/js/filterLongWords.js index e69de29..c8eea94 100644 --- a/js/filterLongWords.js +++ b/js/filterLongWords.js @@ -0,0 +1,23 @@ +var array = ['matt','mosesong','markong'] + +function filteredList(array) { +var maxLength = 2 +var shortArray = [] +for (var i = array.length - 1 ; i>=0 ; i--) { + if(maxLength > array[i].length ) { + shortArray.push(array[i]) + array.splice(i,1) + } + } +// console.log(maxLength); + return maxLength + console.log('hey') + // console.log(shortArray) +} +console.log(filteredList(array)); +// each Array has 3 elements; +// each element we can find their length +// compare length with max length +// if less than than push to new list. +// +// console.log(maxLength) diff --git a/js/fizzbuzz.js b/js/fizzbuzz.js index e69de29..a4fbbae 100644 --- a/js/fizzbuzz.js +++ b/js/fizzbuzz.js @@ -0,0 +1,15 @@ + +function fizzBuzzTest() { + for(var i = 1 ; i <= 100 ; i++) { + if(i % 3 === 0 && i % 5 === 0) { + console.log('fizzbuzz') + } else if(i % 3 === 0) { + console.log('fizz') + } else if(i % 5 === 0) { + console.log('buzz') + } else { + console.log(i) + } + } +} +fizzBuzzTest(); diff --git a/js/grade.js b/js/grade.js index e69de29..87e4bef 100644 --- a/js/grade.js +++ b/js/grade.js @@ -0,0 +1,40 @@ +function inputScore(score) { +switch(true) { + case score >= 85: + console.log('A'); + break; + case score > 75 && score < 85: + console.log('B'); + break; + case score > 65 && score < 75: + console.log('C'); + break; + case score > 55 && score < 65: + console.log('D'); + case score < 55: + console.log('F'); + break; + default: + console.log('Invalid Number. Please try again') + break; +} +} +inputScore(5); + +// define a function that requires a score +// write grades against score +// if grades are below score +// display result +// +// ### grade.js +// Write a program that will print the letter grade, given a +// variable with a test score. Display either "A", "B", "C", +// "D", or "F", for an score that is an integer +// between 0 and 100. +// +// **Requirements** +// * Your program should have a variable to store the letter grade +// // (an integer between 0 and 100) +// * For the letter grades, you may use whatever grading scale you like +// * You must use a switch statement (hint, you may need to review and think +// about how the `switch` statement works) diff --git a/js/phonebook.js b/js/phonebook.js index e69de29..c83277a 100644 --- a/js/phonebook.js +++ b/js/phonebook.js @@ -0,0 +1,45 @@ +// ### phonebook.js +// Use a for...in loop to examine the +// phoneBook Object below and print out the names +// of all the people who share the phone number "333-333-3333". +// ``` +// write a function that search for specific No. +// create a loop that would use specific number to loop and +// search for the No. If can find then print their name +var obj = { + "Abe": "111-111-1111", + "Bob": "222-222-2222", + "Cam": "333-333-3333", + "Dan": "444-444-4444", + "Ern": "555-555-5555", + "Fry": "111-111-1111", + "Gil": "222-222-2222", + "Hal": "333-333-3333", + "Ike": "444-444-4444", + "Jim": "555-555-5555", + "Kip": "111-111-1111", + "Liv": "222-222-2222", + "Mia": "333-333-3333", + "Nik": "444-444-4444", + "Oli": "555-555-5555", + "Pam": "111-111-1111", + "Qiq": "222-222-2222", + "Rob": "333-333-3333", + "Stu": "444-444-4444", + "Tad": "555-555-5555", + "Uwe": "111-111-1111", + "Val": "222-222-2222", + "Wil": "333-333-3333", + "Xiu": "444-444-4444", + "Yam": "555-555-5555", + "Zed": "111-111-1111" +}; +function phoneNumber(number) { + +for (var prop in obj) { + if(number === obj[prop]) { + console.log(prop) +} +} +} +phoneNumber('333-333-3333'); diff --git a/js/reverse.js b/js/reverse.js index e69de29..8c15492 100644 --- a/js/reverse.js +++ b/js/reverse.js @@ -0,0 +1,25 @@ +//Method 1: +function reverseString(string) { + var reverseStr = '' + for(var i = string.length - 1 ; i >= 0 ; i--) { + reverseStr += string[i] + } + console.log(reverseStr) +} +reverseString('building') + +//Method 2 +function reverseString(string) { + var toArray = string.split('') + var newArray = []; + for (var i = string.length - 1 ; i >= 0 ; i--) { + newArray.push(string[i].split('').reverse().join('')) + } +console.log(newArray.join('')) +} +reverseString('building'); + + +// declare a function that reverse string +// convert string into array using Split +// use .reverse .join to join them back. diff --git a/readme.md b/readme.md index 4eeb2a3..c9efe06 100644 --- a/readme.md +++ b/readme.md @@ -16,9 +16,7 @@ Write a program that will take a hardcoded string, and console log the reversed * You must use a `for` loop. No `.reverse()` * You may use the string below -```js -var inputString = 'building'; -``` + ---