Question

In: Computer Science

Part 1(15%): Please write the Boolean expressions below in the file, part5_1.js, in the appropriate functions....

Part 1(15%): Please write the Boolean expressions below in the file, part5_1.js, in the appropriate functions. part5_1.js script will execute if you load the part5_1.html file into the browser.

  1. Write a Boolean expression that checks that number num, has n digits. Assume that num is an integer.( consider both the positive and negative case for num)
  1. Assuming the following are the ingredients for Jamie Oliver's fried rice:
    1. 1/2 dozen green onion
    2. 1 teaspoon chilli jam
    3. 2 cups rice
    4. 2 tablespoon olive oil
    5. 2 eggs
    6. 1/2 block of tofu
    7. 1 teaspoon salt
    8. 1 teaspoon water

Write a Boolean expression that is true, if you need to go shopping for ingredients to make fried rice.

  1. Assuming the following recipe for grilled chicken:
  1. 4 skinless, boneless chicken breasts
  2. 1/2 cup lemon juice or 1/2 lime juice
  3. 1/2 teaspoon onion powder
  4. 2 teaspoons dried parsley or 1 teaspoon dried cilantro
  5. 1 teaspoon salt
  6. 1 teaspoon black pepper

Write a Boolean expression that is true, if you have all the ingredients to make grilled chicken.

part5_1.html :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="part5_1.js"></script>
</head>
<body>
<h1>Boolean expression practice</h1>
Please look at the console. ctrl+shift+i to bring up the developer window.

</body>
</html>

part5_1.js :

console.log("One of the test for question one failed, please check your expression");
}
//Test 1 for 1 digit
if (question1(5, 1)) {
console.log("Testing Question1, okay. Checking for 1 digit")
}
else {
console.log("One of the test for question one failed, please check your expression");
}

//test 2, test for 2 digits
if (question1(99, 2)) {
console.log("Testing Question1, okay. Checking for 2 digit")
}
else {
console.log("One of the test for question one failed, please check your expression");
}

//test 2, test for 2 digits
if (question1(563, 3)) {
console.log("Testing Question1, okay. Checking for 2 digit")
}
else {
console.log("One of the test for question one failed, please check your expression");
}

//test 2, test for 13 digits
if (question1(1000000000234, 13)) {
console.log("Testing Question1, okay. Checking for 2 digit")
}
else {
console.log("One of the test for question one failed, please check your expression");
}
}
function testQuestion2() {
if (typeof (question2(0, 0, 0, 0)) == udefString) {
console.log("function question2 returning undefined, check the return statement.")
return;
}

let ing = {
greenOnion: 8,
chilliJam: 10,
rice: 5,
oliveOil: 2,
egg: 3,
tofu: 1,
salt: 5,
water: 10
}
let keys = Object.keys(ing);
if (question2(ing.greenOnion, ing.chilliJam, ing.rice, ing.oliveOil,
ing.egg, ing.tofu, ing.salt, ing.water) == false) {
console.log("Passed case you dont' need to go shopping")
} else {
console.log("failed case you dont' need to go shopping")
}

for (let i = 0; i < keys.length; i++) {
let tmpIng = JSON.parse(JSON.stringify(ing)); //deep copy object
tmpIng[keys[i]] = 0;
if (question2(tmpIng.greenOnion, tmpIng.chilliJam, tmpIng.rice, tmpIng.oliveOil,
tmpIng.egg, tmpIng.tofu, tmpIng.salt, tmpIng.water) == true) {
console.log("Question 2:Passed case where you need to go shopping for" + keys[i])
} else {
console.log("Question 2:failed case where you need to go shopping for" + keys[i])
}
}

}
function testQuestion3() {
if (typeof (question3(0, 0, 0, 0, 0, 0, 0, 0)) == udefString) {
console.log("function question 3 returning undefined, check the return statement.")
return;
}
let keys = ["breasts", "lemonJuice", "onionPowder", "parsley", "salt", "pepper"]
let ingredients = {
breasts: 5,
lemonJuice: 1,
limeJuice: 0,
onionPowder: 1,
parsley: 3,
cilantro: 0,
salt: 5,
pepper: 5
}

if (question3(ingredients.breasts, ingredients.lemonJuice, ingredients.limeJuice, ingredients.onionPowder, ingredients.parsley, ingredients.cilantro, ingredients.salt, ingredients.pepper) == true) {
console.log("Question 3: Case of having the ingredients passed");
} else {
console.log("Question 3: Case of having the ingredients failed");
}
for (let i = 0; i < keys.length; i++) {
let tempIng = JSON.parse(JSON.stringify(ingredients));
tempIng[keys[i]] = 0;
if (question3(tempIng.breasts, tempIng.lemonJuice, tempIng.limeJuice, tempIng.onionPowder, tempIng.parsley, tempIng.cilantro, tempIng.salt, tempIng.pepper) == false) {
console.log("Question 3: Passed case where you need to go shopping for" + keys[i])
} else {
console.log("Question 3: failed case where you need to go shopping for" + keys[i])
}

}

keys = ["breasts", "limeJuice", "onionPowder", "cilantro", "salt", "pepper"]
ingredients = {
breasts: 5,
lemonJuice: 0,
limeJuice: 2,
onionPowder: 0.5,
parsley: 0,
cilantro: 3,
salt: 5,
pepper: 0
}
for (let i = 0; i < keys.length; i++) {
let tempIng = JSON.parse(JSON.stringify(ingredients));
tempIng[keys[i]] = 0;
if (question3(tempIng.breasts, tempIng.lemonJuice, tempIng.limeJuice, tempIng.onionPowder, tempIng.parsley, tempIng.cilantro, tempIng.salt, tempIng.pepper) == false) {
console.log("Question 3: Passed case where you need to go shopping for" + keys[i])
} else {
console.log("Question 3: failed case where you need to go shopping for" + keys[i])
}

}
}
//Invoke tests
testQuestion1();
testQuestion2();
testQuestion3();

Solutions

Expert Solution

Hello! :)

Here's the script with the completed functions to solve the assignment:

part5_1.js:

function question1(num, n) {
    return n === Math.abs(num).toString().length;
}

function question2(greenOnion, chilliJam, rice, oliveOil, egg, tofu, salt, water) {
    return !(
        greenOnion >= 6
        &&
        chilliJam >= 1
        &&
        rice >= 2
        &&
        oliveOil >= 2
        &&
        egg >= 2
        &&
        tofu >= 1/2
        &&
        salt >= 1
        &&
        water >= 1
    );
}

function question3(breasts, lemonJuice, limeJuice, onionPowder, parsley, cilantro, salt, pepper) {
    return (
        breasts >= 4
        &&
        (lemonJuice >= 1/2 || limeJuice >= 1/2)
        &&
        onionPowder >= 1/2
        &&
        (parsley >= 2 || cilantro >= 1)
        &&
        salt >= 1
        &&
        pepper >= 1
    );
}

function testQuestion1() {
//Test 1 for 1 digit
if (question1(5, 1)) {
console.log("Testing Question1, okay. Checking for 1 digit")
}
else {
console.log("One of the test for question one failed, please check your expression");
}

//test 2, test for 2 digits
if (question1(99, 2)) {
console.log("Testing Question1, okay. Checking for 2 digit")
}
else {
console.log("One of the test for question one failed, please check your expression");
}

//test 2, test for 3 digits
if (question1(563, 3)) {
console.log("Testing Question1, okay. Checking for 3 digit")
}
else {
console.log("One of the test for question one failed, please check your expression");
}

//test 2, test for 13 digits
if (question1(1000000000234, 13)) {
console.log("Testing Question1, okay. Checking for 13 digit")
}
else {
console.log("One of the test for question one failed, please check your expression");
}
}
function testQuestion2() {
if (typeof (question2(0, 0, 0, 0)) == undefined) {
console.log("function question2 returning undefined, check the return statement.")
return;
}

let ing = {
greenOnion: 8,
chilliJam: 10,
rice: 5,
oliveOil: 2,
egg: 3,
tofu: 1,
salt: 5,
water: 10
}
let keys = Object.keys(ing);
if (question2(ing.greenOnion, ing.chilliJam, ing.rice, ing.oliveOil,
ing.egg, ing.tofu, ing.salt, ing.water) == false) {
console.log("Passed case you dont' need to go shopping")
} else {
console.log("failed case you dont' need to go shopping")
}

for (let i = 0; i < keys.length; i++) {
let tmpIng = JSON.parse(JSON.stringify(ing)); //deep copy object
tmpIng[keys[i]] = 0;
if (question2(tmpIng.greenOnion, tmpIng.chilliJam, tmpIng.rice, tmpIng.oliveOil,
tmpIng.egg, tmpIng.tofu, tmpIng.salt, tmpIng.water) == true) {
console.log("Question 2:Passed case where you need to go shopping for" + keys[i])
} else {
console.log("Question 2:failed case where you need to go shopping for" + keys[i])
}
}

}
function testQuestion3() {
if (typeof (question3(0, 0, 0, 0, 0, 0, 0, 0)) == undefined) {
console.log("function question 3 returning undefined, check the return statement.")
return;
}
let keys = ["breasts", "lemonJuice", "onionPowder", "parsley", "salt", "pepper"]
let ingredients = {
breasts: 5,
lemonJuice: 1,
limeJuice: 0,
onionPowder: 1,
parsley: 3,
cilantro: 0,
salt: 5,
pepper: 5
}

if (question3(ingredients.breasts, ingredients.lemonJuice, ingredients.limeJuice, ingredients.onionPowder, ingredients.parsley, ingredients.cilantro, ingredients.salt, ingredients.pepper) == true) {
console.log("Question 3: Case of having the ingredients passed");
} else {
console.log("Question 3: Case of having the ingredients failed");
}
for (let i = 0; i < keys.length; i++) {
let tempIng = JSON.parse(JSON.stringify(ingredients));
tempIng[keys[i]] = 0;
if (question3(tempIng.breasts, tempIng.lemonJuice, tempIng.limeJuice, tempIng.onionPowder, tempIng.parsley, tempIng.cilantro, tempIng.salt, tempIng.pepper) == false) {
console.log("Question 3: Passed case where you need to go shopping for" + keys[i])
} else {
console.log("Question 3: failed case where you need to go shopping for" + keys[i])
}

}

keys = ["breasts", "limeJuice", "onionPowder", "cilantro", "salt", "pepper"]
ingredients = {
breasts: 5,
lemonJuice: 0,
limeJuice: 2,
onionPowder: 0.5,
parsley: 0,
cilantro: 3,
salt: 5,
pepper: 0
}
for (let i = 0; i < keys.length; i++) {
let tempIng = JSON.parse(JSON.stringify(ingredients));
tempIng[keys[i]] = 0;
if (question3(tempIng.breasts, tempIng.lemonJuice, tempIng.limeJuice, tempIng.onionPowder, tempIng.parsley, tempIng.cilantro, tempIng.salt, tempIng.pepper) == false) {
console.log("Question 3: Passed case where you need to go shopping for" + keys[i])
} else {
console.log("Question 3: failed case where you need to go shopping for" + keys[i])
}

}
}
//Invoke tests
testQuestion1();
testQuestion2();
testQuestion3();

I didn't add comments to my part because it is pretty self-explanatory.

Hope this helps! :)


Related Solutions

1. Use Boolean algebra to simplify the following Boolean expressions to expressions containing a minimum number...
1. Use Boolean algebra to simplify the following Boolean expressions to expressions containing a minimum number of literals: (a) A’C’ + A’BC + B’C (b) (A + B + C)’(ABC)’ (c) ABC’ + AC (d) A’B’D + A’C’D + BD (e) (A’ + B)’(A’ + C’)’(AB’C)’ (f) (AE + A’B’)(C’D’ + CD) + (AC)’ 2. Obtain the truth table of the function F = (AB + C)(B + AC), express the function F in sum-of-minterms and product-of-maxterms forms, and express...
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values...
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values to the variables. Print the results. a<b≥c , √a−7 b2 ≠c , d∨e∧f , a<b∨¬d ∧means and, ∨means inclusive or, ¬ means not. b) Write a program that asks a user whether he or she wants to become a Java programmer and determines if the user typed “yes” (Print true if yes and false otherwise.) Don't use the if statement here
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to...
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to print all the numbers from 1 to 120, with two exceptions. For numbers divisible by 4, print "Fizz" instead of the number, and for numbers divisible by 10 (and not 4), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 4 and 10 (and still print "Fizz" or "Buzz" for numbers divisible...
x is 5, what is the result of the following Boolean expressions: 1. x != 0  ...
x is 5, what is the result of the following Boolean expressions: 1. x != 0   2. x > 0   3. x != 0 4. x > 0 5. (x >= 0) || (x < 0) 6. (x != 1) == !(x == 1) 7. (true) && (3 > 4) True or False? Please explain how you got your answers. I've been struggling with Boolean expressions so I'd like a little bit more of an explanation as to why each...
Exercise 6: SOP & Pos Expressions Part 1: For each of the following functions, find all...
Exercise 6: SOP & Pos Expressions Part 1: For each of the following functions, find all of the minimum product of sums expressions: a. f(W,X,Y,Z) = SUMM(2,4,5,6,7,10,11,15) b. f(A,B,C,D) = SUMm(1,5,6,7,8,9,10,12,13,14,15) (1 SOP and 2 POS solutions) Part 2: Find a minimum two-level circuit (corresponding to sum of products expressions) using AND and one OR gate per function foreach of the following sets of functions. a. f(a,b,c,d) = SUMm(0, 1, 2, 3, 4, 5, 8, 10, 13) g(a,b,c,d) = SUMm(0,...
Use an external CSS & JS file, no internal or inline styles & scripts Please comment...
Use an external CSS & JS file, no internal or inline styles & scripts Please comment your JS to break down your understanding of what is happening You can use a CDN link for your jQuery (Links to an external site.) library reference Create a web page that includes these HTML 5 semantic elements: <article> <aside> <figcaption> <figure> <footer> <header> <nav> <section> Your web page should have at least 3 images and they should all use the figure/fig caption elements...
Write Boolean expressions in Python Programming: A currently have a different value than B B less...
Write Boolean expressions in Python Programming: A currently have a different value than B B less than the sum of the current values of A and C C is no more than 15. 75 is between the values of integers A and B Number N is divisible by 2 or it is divisible by 3. X is positive and Y is positive
Write a program to convert a text-file containing expressions (one per line) into post-fix expressions outputted...
Write a program to convert a text-file containing expressions (one per line) into post-fix expressions outputted to a file of your choice using a stack with one space between operators and variables (one letter variables) and/or constants (one digit constants). IN PYTHON please
WRITE IN MYSQL: - Common Table Expressions: What they are, when is the appropriate setting to...
WRITE IN MYSQL: - Common Table Expressions: What they are, when is the appropriate setting to use it, how to write a script for it, and an example of writing that script.
HASKELL PROGRAMMING Use the functions concatenate AND concatList to write the functions below: 1) WRITE the...
HASKELL PROGRAMMING Use the functions concatenate AND concatList to write the functions below: 1) WRITE the function: concatStringsUsingDelimiter (can use fold instruction and a lambda expression to define) Sample run = *Main> concatStringsUsingDelimiter '#' ["The", "quick" , "boy" , "ran."] "The#quick#boy#ran.#" 2) WRITE the function: unconcatStringWithDelimiter (inverse of function above. can use haskell's let construct and/or where construct) Sample run = *Main> unconcatStringWithDelimiter '#' "The#quick#boy#ran.#" ["The" , "quick" , "boy" , "ran."] SIDE NOTE: Please write comments describing each function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT