The Quiz
Date: 11 December 2018
The Prompt
Implement the following function.
/* Prompts the user: "How many points?" * Returns a non-negative integer. * Returns 0 if the user presses cancel or upper * is less than lower. Prompts again if the user * does not enter an integer between lower or zero * (inclusive, whichever is greater) and upper * (exclusive). If lower is not a number, lower * defaults to zero. If upper is not a number, * upper defaults to Infinity. */ function getNumberOfPoints(lower, upper) { } alert(getNumberOfPoints());
Mr. Spurgeon’s Answer
/* Prompts the user: "How many points?" * Returns a non-negative integer. * Returns 0 if the user presses cancel or upper * is less than lower. Prompts again if the user * does not enter an integer between lower or zero * (inclusive, whichever is greater) and upper * (exclusive). If lower is not a number, lower * defaults to zero. If upper is not a number, * upper defaults to Infinity. */ function getNumberOfPoints(lower, upper) { if (lower < 0 || !Number.isInteger(lower)) lower = 0; if (!Number.isFinite(upper)) upper = Infinity; if (upper < lower) return 0; const input = prompt("How many points?"); if (input === null) return 0; const n = parseFloat(input); if (Number.isInteger(n) && n >= 0 && n < upper) return n; return getNumberOfPoints(lower, upper); } alert(getNumberOfPoints());
Rubric
- Does the source code look neat and tidy?
- Do comments contain misspelled words or poor punctuation or capitalization?
- Is indentation used effectively?
- Are variables named well?
- Are constant variables used when the value of a variable should not change?
- Does the style of coding (e.g. capitalization) blend in well with the names of standard JavaScript methods and objects?
- Does the code always run without producing any errors?
- Does the code function correctly?
- Does the function ever return anything other than a non-negative integer?
- Does the function return 0 if the user presses cancel?
- Does the function return 0 if the value of upper is less than lower?
- Does lower default to zero if the value of lower passed to the function is not a number? (How can you tell?)
- Does lower default to zero if the value of lower passed to the function is less than 0? (How can you tell?)
- Does upper default to Infinity if the value of upper passed to the function is not a number? (How can you tell?)
- Does the function re-prompt if the value entered is not an integer between lower and upper?
- What happens if the value entered is a string like "abc"?
- What happens if the value entered is a floating point number like 3.14?
- What happens if the value entered is an integer equal to lower?
- What happens if the value entered is less than lower?
- What happens if the value entered is an integer equal to upper?
- What happens if the value entered is greater than upper?
Students’ Answers
Jaden’s Answer
/* Prompts the user: "How many points?" * Returns 0 if the user preses cancel. * Prompts again if the user does not enter an integer * between lower or zero (inclusive, whichever is * greater) and upper (exclusive). Returns a non- * negative integer. If lower is not a number, * lower defaults to zero. If upper is not a number, * upper defaults to Infinity. */ function getNumberOfPoints(lower, upper) { // Consider using: // prompt, parseFloat, Number.isInteger, Number.isFinite if (lower > upper) { return 0; } if (!Number.isInteger(lower)) { lower = 0; } if (!Number.isInteger(upper)) { upper = Infinity; } let points = prompt("How many points?"); if (points == undefined || points == null) { return 0; } while (!isVaild(points,lower, upper)) { let points = prompt("How many points?"); if (points == undefined || points == null) { return 0; } } return points; } function isVaild(val, lower, upper) { val = parseFloat(val); if (!Number.isInteger(val) || val < lower || val > upper || val < 0 ) { return false; } return true } alert(getNumberOfPoints());
Khanh’s Answer
/* Prompts the user: "How many points?" * Returns 0 if the user presses cancel or upper is less * than lower. * Prompts again if user does not enter an integer * between lower or zero (inclusive, whichever is * greater) and upper (exclusive). Returns a non- * negative integer. If lower is not a number, * lower defaults to zero. If upper is not a number, * upper defaults to Infinity. */ function getNumberOfPoints(lower, upper) { // Consider using: // prompt, parseFloat, Number.isInteger, Number.isFinite if (upper < lower) {return 0}; var num = prompt("How many points?") if (!Number.isInteger(lower)) lower = 0; if (!Number.isInteger(upper)) upper = Infinity; if (num == null || num == undefined) { return 0; } else if (!Number.isInteger(parseFloat(num))) { alert("Please enter a non negative integer between lower and upper"); getNumberOfPoints(lower, upper); } else if (parseFloat(num) < 0 || parseFloat(num) < lower || parseFloat(num) > upper) { alert("Please enter a non negative integer between lower and upper"); getNumberOfPoints(lower, upper); } else {return num} }; alert(getNumberOfPoints(10, 20));
Monisha’s Answer
/*Prompts the user: "How many points?" * Returns 0 if the user presses cancel or upper is less * than lower. * Prompts again if user does not enter an integer * between lower or zero (inclusive, whichever is * greater) and upper (exclusive). Returns a non- * negative integer. If lower is not a number, * lower defaults to zero. If upper is not a number, * upper defaults to Infinity */ function getNumberOfPoints(lower, upper) { // Consider using: // prompt, parseFloat, Number.isInteger, Number.isFinite } x = prompt("How many points?") y = parseFloat (x, 2); if (!Number.isInteger(lower)) lower = 0 if (!Number.isInteger(upper)) upper = Infinity; if ( upper < lower) return 0; if (x == null || x == undefined) { return 0; } alert(getNumberofPoints());
Shaw’s Answer
/*Prompt the user: "How many points?” *Returns 0 if the user presses cancle. *Prompt again if user does not enter an integer *between lower and zero (inclusive, whichever is *greater) and upper (exclusive). Returns a non- *negative integer. If lower is not a number, *lower defaults to zero. If upper is not a number, *upper defaults to Infinity. */ function getNumberOfPoints (lower, upper) { //Consider using //prompt, parseFloat, Number.isInteger, Number.isFinite } alert(getNumberOfPoints()); function getNumberOfPoints (lower, upper) { let x = prompt("how many points?"); if (x === null) alert ("0"); } alert(getNumberOfPoints());
Yuhao’s Answer
/* Prompts the user: "How many points?" * Returns 0 if the user presses cancel. * Prompts again if user does not enter an integer * Between lower or zero (inclusive, whichever is * greater) and upper (exclusive). Returns a non- * negative integer. If lower is not a number, * lower defaults to zero. If upper is not a number, *upper defaults to Infinity */ function getNumberOfPoints(lower,upper) { <script> let x = prompt("How many points?"); if (x === null) alert ("0"); if (x != Number.iInteger); prompt("How many points?") if (x < 0) //Consider using; //prompt, parseFloat, Number.iInteger, Number.isFinite } alert(getNumberOfpoints());
Cici’s Answer
/*Prompts the user:"How many points?" *Return 0 if the user presses cancal. *Prompt again if user does not enter an integer *Between lower or zero(inclusive, whichever is *greater) and upper(exclusive). Returns a non- *negative integer. If lower is not a number, *upper defaults to Infinity. */ let x = window.prompt("How many points?"); if (x === null) alert(0); if (x != Number.isinteger) window.prompt("How many points?"); if (x < 1); alert(5);
Anmita’s Answer
/*Prompts the user: "How many points?" * Returns 0 if the user presses cancel or upper is less * han lower * Prompts again if user does not enter an integer *between lower or zero (inclusive, whichever is *greater) and upper (exclusive). Return a non- *negative integer. If lower is not a number, *lower defaults to zero. if upper is not a number, *upper defaults to Infinity */ function getNumberOfPoints(lower, upper) { // consider using: // prompt, parseFloat, Number.isInteger, Number.isFinite } alert(getNumberOfPoints()); function getNumberOfPoints(lower, upper) { prompt(x) if (x < 0 ) return prompt(x) if ( X = 0 | x > 0 ) { x = lower} prompt (y) if ( y = 0 | y > 0 ) { y = upper} let y = > 0 if ( y < x) return 0; } } // prompt, parseFloat, Number.isInteger, Number.isFinite alert(getNumberOfPoints());
Ethan’s Answer
/* Prompts the user: "How many points?" * Returns 0 if the user presses cancel or upper is less * that lower. * Prompts again if the user does not enter an integer * between lower or zero (inclusive, whichever is * greater) and upper (exclusive). Returns a non- * negative integer. If lower is not a number, * lower defaults to zero. If upper is not a number, * upper defaults to Infinity. */ function getNumberOfPoints(lower, upper) { // Consider using: // prompt, parseFloat, Number.isInteger, Number.isFinite if (!Number.isInteger(lower)) { lower = 0; } if (!Number.isInteger(upper)) { upper = Infinity; } if (upper < lower) { return 0; } var points = prompt("How many points?"); if (points == null || points == undefined) { return 0; } if (parseInt(points) < lower || parseInt(points) > upper) { alert("Please enter a non-negative integer between lower and upper"); getNumberOfPoints(lower,upper); } if (parseFloat(points) < 0) { alert("Please enter a non-negative integer between lower and upper"); } if (!Number.isInteger(parseFloat(points))) { alert("Please enter a non-negative integer between lower and upper"); getNumberOfPoints(lower, upper); } return points; } alert(getNumberOfPoints(10, 20));
Ishaan’s Answer
/* Prompts the user: “How many points?”' * Returns 0 if the user presses cancel. * Pompts again if user does not enter an integer * between lower or zero (inclusive, whichever is great) and upper(exclusive). Returns a non - * negative integer. If lower is not a number, * lower defaults to zero. If upper is not a number, * upper defaults to Infinity */ function getNumberOfPoints(lower, upper) { let answer = prompt("How many points?") parseFloat(answer) if(answer === null) { return 0 } if(answer == undefined) { getNumberOfPoints(lower, upper) } if(!Number.isInteger(parseFloat(answer))) { getNumberOfPoints(); } if(answer < lower || answer > upper) { alert("That number is not within the defined limits") getNumberOfPoints(lower, upper) } alert(answer) // Consider using: // prompt, parseFloat, Number.isInteger, Number.isFinite }