-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.1) Book Fair, Print Car Details & Calculator Assessment 3.js
137 lines (110 loc) · 4.36 KB
/
15.1) Book Fair, Print Car Details & Calculator Assessment 3.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
1) Book Fair:
If the details of a book are provided in the below format:
Key Value
title Malgudi Days
author R. K. Narayan
"alreadyRead" true
isAvailable true
Write code that takes in these book details to perform the following actions:
For each book, use the format, [Book Name] by [Author Name] and print the book title and book author.
You need to print these additional statements to the console based on the provided conditions:
If the user has read the book: You have already read [Book Name] by [Author Name].
If the user has not yet read the book and it is available.": Since [Book Name] by [Author Name] is available, you can read it next.
If the user has not yet read the book, but it is not available: Currently, $[Book Name] by [Author Name] is unavailable.
You can plan to read it next once it becomes available.
Make sure your code is flexible enough to work with any value of the bookDetails variable that meets these requirements.
Solution:
const findTheNextBook = (bookDetails) => {
console.log(`${bookDetails.title} by ${bookDetails.author}`);
if (bookDetails.alreadyRead) {
console.log(`You have already read ${bookDetails.title} by ${bookDetails.author}.`);
}
else {
if (!bookDetails.isAvailable) {
console.log(`Currently, ${bookDetails.title} by ${bookDetails.author} is unavailable. You can plan to read it next once it becomes available.`);
}
else {
console.log(`Since ${bookDetails.title} by ${bookDetails.author} is available, you can read it next.`);
}
}
}
2) Print Car Details:
If the details of a car are provided in the below format:
Key Value
name Tata Nexon
manufacturer Tata
countryOfOrigin India
colour blue
supportElectricOption true
Write code that takes in these car details to perform the following actions:
Print the following welcome message to the console only if the car is Electric: Electric cars are the future! The car we are presenting now is called [Name Here].
If the car manufacturer's origin is in India: This car is made in India.
If not: The country of origin is [Name of the country].
If the car's colour is black: The car's colour is metallic black.
If it is white: The car's colour is snow blush.
If it is neither black nor white: This car is [colour of the car].
Make sure your code is flexible enough to work with any value of the carDetails variable that meets these requirements.
Solution:
const displayCarDetails = (carDetails) => {
if (carDetails.supportElectricOption) {
console.log(`Electric cars are the future! The car we are presenting now is called ${carDetails.name}.`)
};
if(carDetails.countryOfOrigin === "India") {
console.log(`This car is made in India.`);
}
else {
console.log (`The country of origin is ${carDetails.countryOfOrigin}.`);
}
if (carDetails.colour === "black") {
console.log(`The car's colour is metallic black.`)
}
else if (carDetails.colour === "white") {
console.log(`The car's colour is snow blush.`);
}
else {
console.log(`This car is ${carDetails.colour}.`);
};
}
3) Calculator:
Write a code to print the result obtained from the calculation of the provided two numbers, based on the provided operator.
The valueOne and valueTwo variables are numbers to which you need to apply the operator variable.
The supported operators are +, -, *, and /.
For anything else received, print Invalid Operator.
Make sure your code is flexible enough to work with any value of the valueOne, valueTwo and operator variable that meets these requirements.
Solution:
const calculateAndLogValue = (valueOne, valueTwo, operator) => {
switch (operator) {
case "+":
result = valueOne + valueTwo;
break;
case "-":
result = valueOne - valueTwo;
break;
case "*":
result = valueOne * valueTwo;
break;
case "/":
result = valueOne / valueTwo;
break;
default:
result = `Invalid Operator`;
}
console.log(result);
}
4) Good Morning:
Update the function, goodMorning, to make sure it returns Good if the given number is divisible by 3, Morning if divisible by 5, Good Morning
if divisible by both, else return the number itself.
const goodMorning = (number) => {
if (number % 3 === 0 && number % 5 === 0) {
return`Good Morning`;
}
else if (number % 3 === 0) {
return`Good`;
}
else if(number % 5 === 0) {
return `Morning`;
}
else {
return number;
}
}