What does this function do reacjs - reactjs

This code is come from FB ReactNative Movies Demo
function getTextFromScore(score: number): string {
return score > 0 ? score + '%' : 'N/A';
}
what is means ": string" , is return a string ?

This syntax is for the flow type checker. In the argument list you have (score: number) which means the first argument of the function must be a number. After the argument list is the return value of the function. Which is declared as a string.
function getTextFromScore(score: number): string {
return score > 0 ? score + '%' : 'N/A';
}
var x: string = getTextFromScore(5);
Flow is pretty smart, though, so we could remove most of these annotations.
// in no situation will this function not return a string
function getTextFromScore(score: number) {
return score > 0 ? score + '%' : 'N/A';
}
// thus, in no situation will x not be a string
var x = getTextFromScore(5);
I like to type the arguments and return value of a function, but usually not the variables unless I think it adds something, either technically or for readability.

It means the function returns a string.

Yes this means this method will return a string value..
if score is greater then 0 return value by concatenating % with it or return 'NA' when value is less then or equal to 0

function getTextFromScore(score: number): string {
return score > 0 ? score + '%' : 'N/A';
}
function takes score of type number and returns a string
then
if(score > 0 ){score + '%' }else{'N/A'}

Related

While adding two numbers getting NAN as output in the react?

When I am trying to add two numbers, I am able to get NaN. If anyone has any advice, any help would be greatly appreciated.
This is my Code:-
var sum = parseInt(label.labellength, 10) + parseInt(label.labely, 10);
console.log("Sum of FrontRight is " + sum );
Output is:-NaN
I tried as following :
Using Number
var sum=Number(label.labellength) + Number(label.labely);
Output is:-NaN
The same issue came to me , try to use || operator and return it, it did the same and it worked for me...
const [contextOverHr,setContextOverHr=useState("")
return (
{Object.values(workHour).reduce((pre,total) => {
let totalNum = pre + total.OverHour
setContextOverHr (totalNum)
return totalNum || 0
},0)
}
)

Checking existence, react conditional and jsx

I can't help but wonder if there is better way to write this
{numberOfStudents && numberOfStudents > 1 && (
<div>do some jsx</div>
}
Is there a way to make this more succinct?
You can write a function to return the html and then include the function inside the render function.
All three of these return false:
console.log(null > 1)
console.log(undefined > 1)
console.log(0 > 1)
so you do not need the first condition so long as numberOfStudents is always a number | undefined | null.
{numberOfStudents > 1 && <div>...</div>}
Alternatively, if you check an array instead of a number value:
const students = [];
{students?.length > 1 && <div>...</div>}

Sort Numbers after Letters in Angular

I want to sort Letters first and followed by numbers like below:
[Austria , France , Germany , 101110 , 124563]
This is what i have tried:
obj.sort((a,b) => a.text > b.text ? 1 : -1)
But it is sorting numbers first and then letters.
Any help?
If you are looking for a solution like you want to sort string at the first portion of array and numbers at the last of array, just make sure that you are following this procedure.
Never compare a string with a number.
If both a and b are string or both are numbers just compare both with a > b and return 1 or -1 depending on the result of comparision.
The last else block corresponds to the comparision between a number and a string. The return value determines the location of the numbers in the array. If either one of them is not a number, return -1 or 1 depending on the requirement. Since you want the numbers at the end of your array, return -1 if the value is not a number. If you send 1 instead, the numbers will take the first posion in he output array.
const data = [101110 , 124563 , 'France' , 'Austria', 'Germany'];
const output = data.sort((a,b) => {
if (
(isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b))
) {
// Both are strings
// OR
// Both are numbers
return a > b ? 1 : -1;
}
else {
// One of them is a number
return isNaN(a) ? -1 : 1;
}
});
console.log(output);
Much Simplified version
const data = [101110, 'France', 'Austria', 124563, 'Germany'];
const checkOfSamePattern = (a, b) => (isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b));
const output = data.sort((a, b) => checkOfSamePattern(a, b) ? a > b ? 1 : -1 : isNaN(a) ? -1 : 1);
console.log(output);

Calculate sum of props from TextInput

I want to calculate the sum of the props I'm passing to my child component but it seems to only work when I add them in the correct order. Ex. I enter math as 100, science as 100, and so on down the line, it will add correctly. But if I enter math as 100 then art as 100, totalscore will show as 100100 and not 200.
For example:
var totalScore =
this.props.math +
this.props.science +
this.props.history +
this.props.gym +
this.props.lunch +
this.props.art
if (e == 1 || e == 2 || e == 3) {
if (isNaN(totalScore)) {
return totalScore = 'fail';
} else {
return totalScore
}
}
How can I add a value these props in any order with the correct sum?
The problem is not with the order and the order is NOT important. The problem is string concatenation. You are passing string instead of numbers
Explicity typecasting them like below should fix the issue
var totalScore =
Number(this.props.math) +
Number(this.props.science) +
Number(this.props.history) +
Number(this.props.gym) +
Number(this.props.lunch) +
Number(this.props.art)
if (e == 1 || e == 2 || e == 3) {
if (isNaN(totalScore)) {
return totalScore = 'fail';
} else {
return totalScore
}
}
For reference, to pass the props as numbers, do this
<Component someProp={100} >
if you say someProp="100" it will be a string
it seems like the props is a string because the number you enter are concatenated. You can use the parseInt or parseFloat functions, or simply use the unary + operator:
example
var totalScore =
parseInt(this.props.math) +
parseInt(this.props.science)
Or
+this.props.science +
+this.props.math
Or
parseFloat(this.props.math) +
parseFloat (this.props.science)
in short You need to convert the string to number

How to use a self made Type in F#?

I made a type, but I don't know how to use it properly and I don't found any solution on google.
type Sample =
{
TrackPosition : int
TubePosition : int
Barcode : string
}
let arraySamples = Array.create Scenario.Samples.NumberOfSamples **Sample**
BarcodeGenerieren.Samples.Sample
let mutable trackPosition = Scenario.Samples.StartTrackPositions
let mutable index = 1
for i in 1 .. Scenario.Samples.NumberOfSamples do
let randomNumber = System.Random().Next(0,9999)
if index > 24 then
trackPosition <- trackPosition + 1
index <- 1
arraySamples.[index] <- **new Sample{TrackPosition= trackPosition, TubePosition = index, Barcode = sprintf "100%s%06d" ((trackPosition + 1) - Scenario.Samples.StartTrackPositions) randomNumber}**
So my question is, what should I changed so that it works, when I will give the type of the array and when I will give the sample with data to the array?
You have created what is referred to as a record type. You can initialise it with the following syntax
{TrackPosition = 0;TubePosition = 0;Barcode = "string"}
your syntax in the last line is almost correct - it should be
arraySamples.[index] <- Sample{
TrackPosition= trackPosition;
TubePosition = index;
Barcode = sprintf "100%s%06d" ((trackPosition + 1) - Scenario.Samples.StartTrackPositions) randomNumber}
The changes are
Eliminate new
replace , with ;

Resources