Skip to content

Instantly share code, notes, and snippets.

@funador
Created January 27, 2018 00:26
Show Gist options
  • Save funador/0a7a55de5d13c97e62e34715196830f7 to your computer and use it in GitHub Desktop.
Save funador/0a7a55de5d13c97e62e34715196830f7 to your computer and use it in GitHub Desktop.
// =====================================================
// 1.
// Write a function called `doubleIt` which accepts a `num`
// parameter (type: number) and returns double the provided number
// Test #1
// const result1 = doubleIt(10);
// console.log(result1); // => 20
// =====================================================
function doubleIt(num){
return num*2;
}
console.log(doubleIt(20));
// =====================================================
// 2.
// Write a function called `trunkIt` which accepts a `words`
// parameter (type: string) and returns a string where everything
// after the tenth character is removed and replaced with "..."
//
// HINT: The .slice() method and concatenation will be useful here
// Test #2
// const result2 = trunkIt("How much wood would a woodchuck chuck?");
// console.log(result2); // => "How much w..."
// =====================================================
function trunkIt(words){
let re = '...'
return words.slice(0,10)+re;
}
console.log(trunkIt("aaaaaaaaaaa bbbbb"));
// =====================================================
// 3.
// Write a function called `shoutThem` that accepts a `words` parameter
// (type: array) and returns a new array where all the elements have
// been transformed to ALL UPPERCASE.
// Test #3
// const result3 = shoutThem(['how', 'now', 'brown', 'cow']);
// console.log(result3); // => ['HOW', 'NOW', 'BROWN', 'COW']
// =====================================================
function shoutThem(words){
let newArr = [];
for(let i=0; i<words.length; i++){
newArr.push(words[i].toUpperCase());
}
// newArr.push(words.toUpperCase());
return newArr;
}
console.log(shoutThem(["hello", "hi"]));
// =====================================================
// 4.
// If you used a loop to solve the problem above, now use the Array
// .map() method, or vice versa.
// =====================================================
function shoutToThem(words){
return words.map(key => key.toUpperCase());
}
console.log(shoutToThem(["hi", "bye"]));
// =====================================================
// 5.
// Write a function called `evalStudents` that accepts a `students`
// parameter (type: array of objects). Each object has the following
// structure:
//
// { name: 'john', credits: 71, graduated: false }
//
// Your function should check the `credits` prop on each student object.
// If the value is greater than or equal to 90, set the `graduated` prop
// to true. Otherwise, do nothing.
function evalStudents(students){
Object.keys(students).forEach(key => {
if (students[key] >= 90){
}
})
}
// Test #5
const students = [
{ name: 'john', credits: 71, graduated: false },
{ name: 'laura', credits: 95, graduated: false },
{ name: 'rich', credits: 36, graduated: false },
{ name: 'tauhida', credits: 90, graduated: false }
];
evalStudents(students);
console.log(students);
// =====================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment