Wednesday, June 3, 2020

#1 2020-03-23 03:16:04 am

bez9199
Member
Registered: 2019-06-11
Posts: 2

How Do I Use The 'Or' Function

I am creating a nifty program that tells you if the number you enter is divisible by the numbers 1-9. I'm currently on the section that tells if it's divisible by 5. The trick is if the last digit in the number ends in a 5 or 0, the number is divisible by 5. I tried using this to see if I could make it work:

Applescript:

-- Determines if initial number is divisible by 5
if (last character of numString) = 0 or 5 then
   set fiveCheck to "true"
else
   set fiveCheck to "false"
end if

However, when I run this, I get an error from the editor saying: 'error "Can’t make 5 into type boolean." number -1700 from 5 to boolean'. I'm fairly new to AppleScript and it'd be great if I could get some help on this.

*Some important things to note are that the variable 'numString' is just the text returned of a dialog where you enter in the number you want.


Filed under: , applescript, functions, Booleans

Offline

 

#2 2020-03-23 03:33:28 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: How Do I Use The 'Or' Function

Applescript:


-- Script: Check the divisibility of a real or integer by 5, without increasing the signs of the remainder.

set aNumber to 625.5 -- real or integer number
set numString to aNumber as text -- coerce to string
set lastChar to last character of numString -- last character

if (lastChar = "0") or (lastChar = "5") then -- compare string with string (not string with number), OR statement has this syntax (the results of left and right operands should be boolean), that is, we compare is TRUE one of 2 conditions or not
   set fiveCheck to true -- boolean result, not string
else
   set fiveCheck to false -- boolean result, not string
end if

Other way:

Applescript:


-- Script: Check the divisibility of a real or integer by 5, without increasing the signs of the remainder.
set aNumber to 625.5

if last character of (aNumber as text) is in {"0", "5"} then
   set fiveCheck to true
else
   set fiveCheck to false
end if

Or:

Applescript:


-- Script: Check the divisibility of a real or integer by 5, without increasing the signs of the remainder.
set aNumber to 625.5

if item -1 of (aNumber as text) is in {"0", "5"} then
   set fiveCheck to true
else
   set fiveCheck to false
end if

And finally, a joke: Adding the TRUE to the OR statement turns its result into an “eternal” TRUE:

Applescript:


if ("Elefant" is "Mickey mouse") or true then return "Yep, elefant is Mickey mouse"
--> RESULT: "Yep, elefant is Mickey mouse"

if ("Elefant" is not "Mickey mouse") or true then return "Yep, elefant is Mickey mouse"
--> RESULT: "Yep, elefant is Mickey mouse"

Last edited by KniazidisR (2020-03-24 02:37:03 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-03-23 07:55:53 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 482

Re: How Do I Use The 'Or' Function

bez9199 wrote:

I am creating a nifty program that tells you if the number you enter is divisible by the numbers 1-9.



bez9199. Welcome to the forum.

The question you ask is about the operator "or" and this question has been fully answered by KniazidisR. FWIW, the following is a different approach to the intended purpose of the script, although it only works with whole numbers.

Applescript:

display dialog "Enter a number to divide:." default answer ""
set theDividend to (text returned of result) as integer
if theDividend = 0 then error number -128

set theResult to ""

repeat with theDivisor from 1 to theDividend -- change theDividend to 9 to limit divisor to 9
   if (theDividend mod theDivisor) = 0 then
       set theResult to theResult & theDivisor & ", "
   end if
end repeat

display dialog "The number " & (theDividend as text) & " is divisible by the numbers " & (text 1 thru -3 of theResult) buttons "OK" cancel button 1 default button 1

Last edited by peavine (2020-03-24 09:50:28 am)


2018 Mac mini - macOS Catalina

Offline

 

#4 2020-03-23 02:13:28 pm

bez9199
Member
Registered: 2019-06-11
Posts: 2

Re: How Do I Use The 'Or' Function

Thank you a lot for your detailed and helpful answers! I really appreciate it.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)