-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_mini_exercises.html
More file actions
84 lines (63 loc) · 2.65 KB
/
js_mini_exercises.html
File metadata and controls
84 lines (63 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mini exercises</title>
</head>
<body>
<script>
"use strict";
//================ !! Mini-exercises !!
// Write a function, returnFive, that returns the number five. No inputs should be defined.
//function returnFive() {
// return 5;
//}
// console.log(returnFive());
// Write a function, isFive, that takes in an input and returns the boolean value true if the passed argument is the number 5 or the string "5". Return false otherwise.
//how many inputs should the function have? 1
//what are the data types for the inputs? number, string, any data type
//what is the data type of the output/return value boolean
// function isFive(input) {
// return input == 5;
// }
// console.log(isFive('5'), true);
// console.log(isFive(5), true);
// console.log(isFive('zzz'), false);
// console.log(isFive(33), false);
// Write a function, isShortWord, that takes in a string and returns the boolean value true if the passed argument is shorter than 5 characters. Return false otherwise.
//how many inputs? 1
// what data type for inputs? string
// what is the return type? boolean
function isShortWord(stringInput) {
return stringInput.length <5;
}
console.log(isShortWord('password'), false);
console.log(isShortWord('five'), true);
console.log(isShortWord('233'), true);
console.log(isShortWord('aaaaa'), false);
// Write a function, isSameLength, that takes in two string inputs and returns the boolean value true if the passed arguments are the same length. Return false otherwise.
function isSameLength (str1, str2) {
// create variable to store boolean comparing strings
var stringsAreSameLength = str1.length === str2.length
//return variable outout
return stringsAreSameLength
}
// example of inputs and outputs
console.log(isSameLength('cat', 'hat'), true);
console.log(isSameLength('dog', 'plane'), false);
console.log(isSameLength(' ', 'dog'), true);
// Write a function, getSmallerSegment, that takes in a string and a number input. The function should return a substring of the first argument that is as many characters long as the second argument in lowercase.
// example input: getSmallerSegment("Codeup", 3)
// example output: "cod"
// how many inputs? 2
// what types of inputs? string, number
// what type of output? string
function getSmallerSegment(str, segmentLength) {
return str.substring(0, segmentLength).toLowerCase();
}
console.log(getSmallerSegment('dog', 1), 'd');
console.log(getSmallerSegment('Hello', 2) 'he');
console.log(getSmallerSegment('seventeen' 5), 'seven');
</script>
</body>
</html>