Menu
Hi all
I have the following function:
`function finalGrade(num1, num2, num3) {
if ((num1 < 0 || num1 > 100) || (num2 < 0 || num2 > 100) || (num3 < 0 || num3 > 100)) {
return ‘You have entered an invalid grade’;
} else {
const average = ((num1 + num2 + num3) / 3);
}
if (average <= 59) {
return ‘F’;
} else if (average <= 69) {
return ‘D’;
} else if (average <= 79) {
return ‘C’;
} else if (average <= 89) {
return ‘B’;
} else if (average <= 100) {
return ‘A’
}
};
console.log(finalGrade(50, 100, 60));
But this code for some reason that I cant figure it out its not working, could anyone give me a hand…
var:global scope(able to be used anywhere in the code)[/quote]
function someFunc() {
var someVar = 124;
}
someFunc();
console.log(someVar);
``<i>
</i>function finalGrade (num1, num2, num3) {
if ((num1 < 0 || num1 > 100) || (num2 < 0 || num2 > 100) || (num3 < 0 || num3 > 100)) {
return 'You have entered an invalid grade'
}
// will only get to here if the numbers are valid
const average = ((num1 + num2 + num3) / 3)
if (average <= 59) {
return 'F'
} else if (average <= 69) {
return 'D'
} else if (average <= 79) {
return 'C'
} else if (average <= 89) {
return 'B'
} else if (average <= 100) {
return 'A'
}
}<i>
</i>
``
``<i>
</i>function bar() {
myGlobal = 5
}
bar()
console.log(myGlobal) // 5<i>
</i>
`</CODE>
standard practice though would be to define your global on window
e.g.
<CODE>
`<i>
</i>(function(){
const myApp = // ... code here
window.myApp = myApp // exposed to the global namespace
}())<i>
</i>
`</CODE>
When a function is invoked/executed the var variables are hoisted to the top and are initially assigned a value of 'undefined'
The variable is only assigned a value, when the function execution reaches that variable declaration. The same can be said in the global namespace e.g.
<CODE>
`<i>
</i>console.log(x) // undefined
var x = 5
console.log(x) // 5<i>
</i>
`</CODE>
As mentioned var is lexically scoped, which means in the case of a function invocation, if you try to use a variable that hasn't been defined inside that function it will look up the scope chain to a wrapping parent functions to find it, if it is not found there then to the next parent all the way up to the global namespace
<CODE>
`<i>
</i>function parent() {
var x = 5
function child() {
// will not be able to find x in child so looks up the scope chain to parent
console.log(x)
}
child()
}
parent() // 5<i>
</i>
`</CODE>
or
<CODE>
`<i>
</i>var x = 10 // defined in the global namespace
function parent() {
// x isn't found here so looks up the scope chain to the global namespace
function child() {
// will not be able to find x in child so looks up the scope chain to parent
console.log(x)
}
child()
}
parent() // 10<i>
</i>
`</CODE>
<QUOTE><i>> </i><POSTMENTION discussionid="391478" displayname="nigh-knight" id="1624080" number="7" username="nigh-knight">@nigh-knight#1624080</POSTMENTION> only const can be reassigned</QUOTE>
Will take that as a typo, it <STRONG>**can't**</STRONG> be reassigned. However in the case of an object type it can still be mutated e.g.
<CODE>
`<i>
</i>const myArray = [1,2,3]
myArray.push(4) // [1,2,3,4]<i>
</i>
`</CODE>
There is another distinct difference, <STRONG>**let**</STRONG> and <STRONG>**const**</STRONG> cannot be used ahead of their definition and will throw an error
<CODE>
`<i>
</i>console.log(x) // Uncaught ReferenceError: x is not defined
let x = 5 <i>
</i>
`</CODE>
As you see above with var and 'undefined' that isn't the case.
Edit: What has just dawned on me and where we are maybe getting crossed wires, is that var is not block scoped like const and let. e.g.
<CODE>
`<i>
</i>var x = 5
if (x === 5) {
var y = 10
}
console.log(y) // 10<i>
</i>
``
<script><br/>
function finalGrade (num1, num2, num3) {
function isOutOfRange(num) {<br/>
let flag = isNaN(num); // true if undefined entry<br/>
if ((num < 0 | num > 100)) { flag = true; } // out of range entry<br/>
return flag;<br/>
}
if (isOutOfRange(num1) | isOutOfRange(num2) | isOutOfRange(num3)) {<br/>
return 'You have entered an invalid grade'<br/>
}<br/>
// will only get to here if the numbers are valid<br/>
const average = ((1<EM>*num1 + 1*</EM>num2 + 1*num3) / 3) // any str numbers to numbers
if (average < 60) { return 'F'; } // does not round up from 59 to 60<br/>
if (average < 70) { return 'D'; }<br/>
if (average < 80) { return 'C'; }<br/>
if (average < 90) { return 'B'; }<br/>
return 'A'; // all checks made of grade<br/>
}
// test actions<br/>
alert(finalGrade(70,70,70)); // valid C<br/>
alert(finalGrade(70,-70,70)); // invalid entry<br/>
alert(finalGrade('70','70','70')); // valid C<br/>
alert(finalGrade(70,'C',70)); // invalid entry<br/>
alert(finalGrade(69,70,71)); // valid C
alert(finalGrade(59,60,60)); // F<br/>
alert(finalGrade(69,70,70)); // D<br/>
alert(finalGrade(79,80,80)); // C<br/>
alert(finalGrade(89,90,90)); // B<br/>
alert(finalGrade(99,100,100)); // A<br/>
</script>
// will only get to here if the numbers are valid<br/>
const average = ((num1*1 + num2*1 + num3*1) / 3) // any str numbers to numbers
``<i>
</i>// every will return false if any scores fail the test
function validScores (...scores) {
return scores.every(score => score >= 0 && score <= 100)
}
// using rest parameters e.g. (...nums) any number of
// arguments are converted to an array
function average (...nums) {
return nums.reduce((x, y) => x + y) / nums.length
}
function finalGrade (grades = [], score1, score2, score3) {
// if !not valid scores return
if (!validScores(score1, score2, score3)) {
return 'You have entered an invalid score'
}
const averageScore = average(score1, score2, score3)
...
}<i>
</i>
``
<!DOCTYPE html><html lang="en"><head><br/>
<meta charset="UTF-8"><br/>
<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=yes"/><br/>
<title> Test Page </title><br/>
<!-- From: <URL url="https://www.webdeveloper.com/d/391478-function-issue/9">https://www.webdeveloper.com/d/391478-function-issue/9</URL> --><br/>
</head><body>
<pre id='demo'></pre>
<script><br/>
console.clear();
function finalGrade () {
function isOutOfRange(num) {<br/>
let flag = isNaN(num); // true if undefined entry<br/>
if ((num < 0 | num > 100)) { flag = true; } // out of range entry<br/>
return flag;<br/>
}
// arguments is part of all functions<br/>
var nums = [...arguments], sum = 0, err = false;<br/>
for (let i=0; i<nums.length; i++) {<br/>
if (isOutOfRange(nums<I>[i])) { err = 'You have entered an invalid grade' }<br/>
else { sum += nums<I>[i]*1; } // summation in same loop<br/>
}<br/>
if (err) { return err; } // input error found<br/>
<br/>
const average = (sum / nums.length);<br/>
let grade = 'A';<br/>
if (average < 90) { grade = 'B'; } // does not round up from 59 to 60<br/>
if (average < 80) { grade = 'C'; }<br/>
if (average < 70) { grade = 'D'; }<br/>
if (average < 60) { grade = 'F'; }<br/>
<br/>
var result = [];<br/>
result.push(grade,average.toFixed(1)); // all checks made of grade<br/>
return result;<br/>
}<br/>
<br/>
// test actions - can get letter grade with [0] or average grade with [1]<br/>
document.getElementById('demo').append(finalGrade(70,70,70)+'n'); // valid C<br/>
document.getElementById('demo').append(finalGrade(70,-70,70)+'n'); // invalid entry<br/>
document.getElementById('demo').append(finalGrade('70','70','70')+'n'); // valid C<br/>
document.getElementById('demo').append(finalGrade(70,'C',70)+'n'); // invalid entry<br/>
document.getElementById('demo').append(finalGrade(69,70,71)+'n'); // valid C<br/>
<br/>
document.getElementById('demo').append(finalGrade(59,60,60,60,60,60,60)+'n'); // F<br/>
document.getElementById('demo').append(finalGrade(69,70,70,70,70,70,70)+'n'); // D<br/>
document.getElementById('demo').append(finalGrade(79,80,80,80,80,80,80)+'n'); // C<br/>
document.getElementById('demo').append(finalGrade(89,90,90,90,90,90,90)+'n'); // B<br/>
document.getElementById('demo').append(finalGrade(99,100,100,100,100,100,100)+'n'); // A<br/>
<br/>
document.getElementById('demo').append(finalGrade(70,80,90,100,75,85,95)+'n'); <br/>
<br/>
</script><br/>
</body></html><br/>
```
// every will return false if any scores fail the test
function validScores (scores) {
return scores.every(score => score >= 0 && score <= 100)
}
function average (nums) {
return nums.reduce((x, y) => x + y) / nums.length
}
function finalGrade (grades = [], ...scores) {
// if !not valid scores return
if (!validScores(scores)) {
return 'You have entered an invalid grade'
}
const averageScore = average(scores)
// using destructuring to get the indexed values
// e.g. [grade, score] = ['B', 80] -> grade = 'B' score = 80
const [grade] = grades.find(([grade, score]) => averageScore >= score)
return grade
}
// could have used Map possibly
const grades = [['A', 90], ['B', 80], ['C', 70], ['D', 60], ['F', 0]]
console.log(finalGrade(grades, 59, 60, 60, 50)) // F
console.log(finalGrade(grades, 69, 70, 70)) // D
console.log(finalGrade(grades, -79, 80, 80)) // You have entered an invalid grade
console.log(finalGrade(grades, 89, 90, 90, 75, 60)) // B
console.log(finalGrade(grades, 99, 100, 100)) // A
0.1.9 β BETA 4.23